View Full Version : using free()

01-26-2002, 07:31 PM
hi again...

I have a strcuture:

struct CHAR_DATA {
CHAR_DATA *next;
CHAR_DATA *prev;

char name[12];

I have it set up in a double linked list. when removeing one, I unlink it from a list and then free it. but for some reason when I free it, it makes the program crash. However, if I dont everything runs fine.

What woudl happen if I don't free() the structure? and why woudl it be making the program crash? any suggestions on fxing it? thanks in advance.

btw, it dumps a core, but I forgot how to read the core file. I'm uisng a linux server that I log into using telnet. Its been a while since I last used linux so I forgot some of the commands.

Thanks again.

01-27-2002, 03:40 AM
Are you sure you are freeing the correct element? You have to free the deleted nodes because you want to claim the memory allocated to them. If you don't free the deleted elements, you will leak out the memory.

If you post your code, we will help you more. I think your problem is in somewhere else and probably you are deleting the wrong node.

01-27-2002, 12:35 PM
(Also known as SPEKTRUM)

Heres my code:

void close_connection(CONNECTION_DATA *con)
if (con->ch) {

log_to_file(LOG_NORMAL,"Connection closed on socket #%d",con->sd);
con = NULL;

and heres the function kill_ch:

void kill_ch(CHAR_DATA *ch)
log_to_file(LOG_NORMAL,"Character %s(%s) on socket #%d was removed.",
ch->name, ch->con->ip_addr, ch->con->sd);

When I free(ch), it frees ch just fine. but when I free(con) it crashes the program. any help? Thanks.

01-27-2002, 11:31 PM
have you used a debugger? That would be the way i would go. you'd find the bug with one...i think gdb and ddd are pretty standard with the major linux distros...