Thread: Problem Deleting Part of a Linked List

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    47

    Problem Deleting Part of a Linked List

    Hello,

    I am writing an IRC-like chat server but have come up onto a problem. I have a class called cChannelHandler that handles all the channel issues. I ran a test script that created 20000 Channels which are structures calles sChannels. These structures are linked lists. Creating them goes fine. according to /proc/pid/status the memory usage does rise. But when I delete the lists the memory usage is still increasing according to proc. I'm wondering if this is just a bug in linux or something I am doing wrong.

    Below is a snippet of the RemoveChannel Function
    Code:
    	sChannels *tempchan;
    	sChannels *tempchan2;
    	tempchan = GetChannel(*channum); //Returns A Channel Pointer
    	if(*channum == 0)
    	{
    		tempchan = ChannelList->next;
    		ChannelList->next=NULL;
    		delete ChannelList;
    		ChannelList = tempchan;
    		numchannels--;
    	}
    	else
    	{
    	tempchan2 = tempchan->next;
    	tempchan->next = NULL;
    	delete tempchan;
    	GetChannel((*channum) - 1)->next = tempchan2;
    	numchannels--;
    	}

    Please help me out here. what is causing this program to increase memory usage when I delete.

  2. #2
    Registered User
    Join Date
    Jul 2002
    Posts
    47

    Oh

    Here is the structure definition of sChannels btw.


    Code:
    struct sChannels //Structure Used For Storing Channel Information
    {
    	sUserList *UserList; //Pointer To Channel UserList
    	sBanList *BanList; //List of Banned Users
    	char *ChannelName; //Name of Channel
    	char *Password; //Channel Password
    	int chantype; //0-Normal 1-Voice 2-Both 1&2
    	bool ReservedOnly; //Channel For Reserved Users Only
    	int codec; //Codec 1=Low 2=Mid-Low 3-Mid 4-Mid-High 5-High
    	char *Topic; //The Channel Topic
    	int maxusers; //Max Users Allowed in Channel
    	bool RequireSpeakPermissions; //Determines if SpeakPermissions Are Req
    	sChannels *next; //Pointer for Linked List
    	sChannels() //Constructor
    	{
    		UserList = NULL;
    		BanList = NULL;
    		ChannelName = NULL; //Set in Channel Handler
    		Password = NULL; //Set in Channel Handler NULL=public
    		chantype = 0; //Normal Channel
    		ReservedOnly = false; //Public Channel
    		codec = 1; //Low Quality Default
    		Topic = NULL; //Set in Channel Handler
    		maxusers = 0; //Set in Channel Handler
    		RequireSpeakPermissions = false; //Default to False
    		next = NULL;
    	};
    	~sChannels() //Destructor
    	{
    		if(UserList != NULL)
    			delete UserList;
    		if(BanList != NULL)
    			delete BanList;
    		if(ChannelName != NULL)
    			delete [] ChannelName;
    		if(Password != NULL)
    			delete [] Password;
    		if(Topic != NULL)
    			delete [] Topic;
    		if(next != NULL)
    			delete next;
    	};
    };

  3. #3
    C > C++ duders ggs's Avatar
    Join Date
    Aug 2001
    Posts
    435
    i couldn't see a problem when i scanned your code, so..:
    create your 20000 channels, delete them, wait for a keystroke, then reallocate those 20000 channels. if memory usage continues to rise on the second pass, then you probably have a problem.
    .sect signature

  4. #4
    Registered User
    Join Date
    Jul 2002
    Posts
    47
    Thats what I am doing and it still happens. This is sooo weird. Although I let the loop run an infinate creation of 150000 channels and removal of 150000 channels loop and it ran successfully all day and all night.

  5. #5
    C > C++ duders ggs's Avatar
    Join Date
    Aug 2001
    Posts
    435
    how do you delete your channels? are you removing them from the head of the list?

    although if it runs all day and night successfully, it doesn't seem that memory usage should be able to increase throughout this proportionally to what you're doing here...

    suggest just delete-ing the head of the list without using RemoveChannel in your allocation loop to see what happens, that seems airtight
    .sect signature

  6. #6
    Registered User
    Join Date
    Jul 2002
    Posts
    47
    heh I think I figured it out. In the Remove Channel Function i have an
    int *channum = new int;

    I delete it on all my return false; statements. But forgot to tell it to delete it when it returns true; (succeeds) lol That would be an increase of 4bytes per channel removal

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked List Not Saving Value as Int
    By bar338 in forum C Programming
    Replies: 4
    Last Post: 05-04-2009, 07:53 PM
  2. I need help on this particular Linked List problem
    By sangken in forum C Programming
    Replies: 11
    Last Post: 08-06-2006, 12:26 AM
  3. Linked list problem
    By mr_glass in forum C Programming
    Replies: 4
    Last Post: 03-07-2006, 02:12 AM
  4. Linked List
    By jpipitone in forum C Programming
    Replies: 4
    Last Post: 03-30-2003, 09:27 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM