Thread: Plugging memory leaks

  1. #1
    Registered User
    Join Date
    Sep 2006
    Location
    vancouver wa
    Posts
    221

    Plugging memory leaks

    I have detected 3 leaks that i dont know how to get rid of. could someone look it over and see if they can tell what i am doing wrong?

    3 leaks:
    \lab1\winery.cpp (22): winery::winery
    \lab1\list.cpp (286): list::node::node
    \lab1\list.cpp (125): list::insert

    Code:
    list::~list()
    {
    
    
    	node * curr = headByName;
    	while(headByName)
    	{
    	curr = headByName->nextByName;
    	delete headByName;		
    	headByName = curr;
    	} 
    
    
    
    }
    Code:
    winery::~winery()
    {
    
    	if(name)
               delete[] name;
    	
    	
    	if(location)
    	delete[] location;
    	
    
    }
    Now the functions where its showing im having the trouble..

    Code:
    winery::winery(const char * const name, const char * const location, const int acres, const int rating): name( new char[strlen(name)+1] ), location( new char[strlen(location)+1] ),acres(0),rating(0)
    {
    
    	strcpy( this->name,  name );
    	strcpy( this->location, location );
    	this->acres = acres;
    	this->rating = rating;
    	
    	
    
    }

    Code:
    list::node::node(const winery &winery) : item(winery.getName(), winery.getLocation(), winery.getAcres(), winery.getRating())
    
    {
    
    
    }
    I know i am creating memory here with new.. However, it wont let me delete that..

    Code:
    void list::insert(const winery& winery)
    {
    
    	node* wineryNode = new node(winery);    // winerynode for adding and sorting name
    	node* prev = NULL;
    	node* prevRating = NULL;
    	node* check = headByName;
    	node* checkRating = headByRating;
    
    
    
    	if ( headByName == NULL ) 
    	{       
    		headByName   = wineryNode;  
    	}
    
    	if ( headByRating == NULL)
    	{
    		headByRating = wineryNode; 
    	}
    
    
    	while (check != NULL && (strcmp(check->item.getName(), wineryNode->item.getName() ) < 0))
    	{
    
    		prev = check;
    		check = check->nextByName;
    	}
    
    	if (prev == NULL)
    	{
    		headByName = wineryNode;
    	}
    	else
    	{
    		prev->nextByName = wineryNode;
    	}
    	wineryNode->nextByName = check;  
    
    
    
    	while (checkRating != NULL && (checkRating->item.getRating() > wineryNode->item.getRating()))
    
    	{
    		prevRating = checkRating;
    		checkRating = checkRating->nextByRating;
    	}
    
    	/// Sort for Rating *******************
    	if (prevRating == NULL)
    	{
    		headByRating = wineryNode;
    	}
    	else
    	{
    		prevRating->nextByRating = wineryNode;
    	}
    
    	wineryNode->nextByRating = checkRating;
    
    }
    [/CODE]

  2. #2
    Registered User
    Join Date
    Sep 2006
    Location
    vancouver wa
    Posts
    221
    I took out the call to this function and the leaks were gone.. What am i doing wrong?

    Code:
    bool list::remove (const char * const name)
    {
    
    	node * prev = NULL;
    	node * prevRating = NULL;
    	node * p = NULL;
    	node * d = NULL;
    	node * curr = headByName;
    	node * currRating = headByRating;
    
    	while(curr)
    
    	{
    		if(strcmp(curr->item.getName(), name) == 0)
    		{
    			prev = headByName;
    			p = prev->nextByName;
    			headByName = p;
    		
    		}
    		curr = curr->nextByName;
    	}
    
    
    	while(currRating)
    	{
    		if(strcmp(currRating->item.getName(), name) == 0)
    		{
    
    			prevRating = headByRating->nextByRating->nextByRating->nextByRating;
    			d = prevRating->nextByRating;
    			prevRating->nextByRating = d->nextByRating;
    
    		}
    		currRating = currRating->nextByRating;
    	}
    
    	prev = NULL;
    	delete prev;
    	d = NULL;
    	delete d;
    	return true;
    
    }

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Code:
    	prev = NULL;
    	delete prev;
    	d = NULL;
    	delete d;
    Deleting NULL pointers is a no-op (in your other pieces of code there's no point to check for NULL first, either).
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    Registered User
    Join Date
    Sep 2006
    Location
    vancouver wa
    Posts
    221
    Never mind i got it fixed.. is this better?

    Code:
    bool list::remove (const char * const name)
    {
    
    	node * prev = NULL;
    	node * prevRating = NULL;
    	node * p = NULL;
    	node * d = NULL;
    	node * curr = headByName;
    	node * currRating = headByRating;
    
    	while(curr)
    
    	{
    		if(strcmp(curr->item.getName(), name) == 0)
    		{
    			prev = headByName;
    			p = prev->nextByName;
    			headByName = p;
    		
    		}
    		curr = curr->nextByName;
    	}
    
    
    	while(currRating)
    	{
    		if(strcmp(currRating->item.getName(), name) == 0)
    		{
               prevRating = headByRating->nextByRating->nextByRating->nextByRating;
    			//prevRating = headByRating->nextByRating->nextByRating->nextByRating;
    			d = prevRating->nextByRating;
    			prevRating->nextByRating = d->nextByRating;
    			prevRating = prev;
    
    		}
    		currRating = currRating->nextByRating;
    	}
    
    
    	delete prev;
    	return true;
    
    }

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    PLEASE DON'T CROSS POST!
    I just answered some of this in your other thread!

    There are a few things wrong with this constructor:
    Code:
    winery::winery(const char * const name, const char * const location, const int acres, const int rating)
     : name( new char[strlen(name)+1] ), location( new char[strlen(location)+1] ), acres(0), rating(0)
    {
    
    	strcpy( this->name,  name );
    	strcpy( this->location, location );
    	this->acres = acres;
    	this->rating = rating;
    }
    First of all, the point of an initialistion list is to directly initialise a member to the desired value. Don't initialise it to zero and then assign to it later, as that defeats the purpose. It is perfectly safe to do "acres(acres)" even though the member name is the same as the constructor argument name.
    Don't use 'new' more than once in a constructor initialisation list. If new throws, then you're hosed. There is a GotW article about that.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking for memory leaks
    By Bladactania in forum C Programming
    Replies: 5
    Last Post: 02-10-2009, 12:58 PM
  2. memory leaks
    By TehOne in forum C Programming
    Replies: 4
    Last Post: 10-10-2008, 09:33 PM
  3. Tons of memory leaks
    By VirtualAce in forum C++ Programming
    Replies: 11
    Last Post: 12-05-2005, 10:19 AM
  4. COM Memory Leaks
    By subdene in forum Windows Programming
    Replies: 0
    Last Post: 06-07-2004, 11:57 AM
  5. about memory leaks with this simple program
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 04-07-2002, 07:19 PM