PDA

View Full Version : using free()



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

I have a strcuture:

struct CHAR_DATA {
CHAR_DATA *next;
CHAR_DATA *prev;
CONNECTION_DATA *con;

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.

ozgulker
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.

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

Heres my code:



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

UNLINK(con,first_con,last_con,next,prev);
close(con->sd);
log_to_file(LOG_NORMAL,"Connection closed on socket #%d",con->sd);
free(con);
con = NULL;
}


and heres the function kill_ch:



void kill_ch(CHAR_DATA *ch)
{
UNLINK(ch,first_ch,last_ch,next,prev);
log_to_file(LOG_NORMAL,"Character %s(%s) on socket #%d was removed.",
ch->name, ch->con->ip_addr, ch->con->sd);
free(ch);
}


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

mix0matt
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...