C Board  

Go Back   C Board > Platform Specific Boards > Linux Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 01-26-2002, 07:31 PM   #1
SPEKTRUM
Guest
 
Posts: n/a
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.
  Reply With Quote
Old 01-27-2002, 03:40 AM   #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
ozgulker is offline   Reply With Quote
Old 01-27-2002, 12:35 PM   #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.
__________________
[Strut]

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

Last edited by Strut; 01-27-2002 at 12:38 PM.
Strut is offline   Reply With Quote
Old 01-27-2002, 11:31 PM   #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.
mix0matt is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
(C, Malloc, Free) Help! Access violation and/or damage after normal block!! Raptor007 C Programming 12 06-24-2005 04:27 PM
Help needed with backtracking sjalesho C Programming 1 11-09-2003 06:28 PM
"if you love someone" :D Carlos A Brief History of Cprogramming.com 12 10-02-2003 01:10 AM
SIGABRT upon free() registering C Programming 2 07-19-2003 07:52 AM


All times are GMT -6. The time now is 09:54 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22