Thread: using free()

  1. #1
    SPEKTRUM
    Guest

    using free()

    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.

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    65
    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.
    The experimenter who does not know what he is looking for will not understand what he finds.
    - Claude Bernard

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    44

    my code

    (Also known as SPEKTRUM)

    Heres my code:

    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:

    Code:
    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.
    Last edited by Strut; 01-27-2002 at 12:38 PM.
    [Strut]

    I lay on my bed watching the stars, and I thought to myself... Where the hell is my roof?

  4. #4
    junior member mix0matt's Avatar
    Join Date
    Aug 2001
    Posts
    144
    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...
    THIS IS NOT JUST A CHRONICLING OF THINGS WE HAVE DONE IN THE PAST BUT OUR RISE TO POWER.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 06-24-2005, 04:27 PM
  2. Help needed with backtracking
    By sjalesho in forum C Programming
    Replies: 1
    Last Post: 11-09-2003, 06:28 PM
  3. "if you love someone" :D
    By Carlos in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 10-02-2003, 01:10 AM
  4. SIGABRT upon free()
    By registering in forum C Programming
    Replies: 2
    Last Post: 07-19-2003, 07:52 AM