-
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]
-
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;
}
-
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).
-
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;
}
-
PLEASE DON'T CROSS POST!
I just answered some of this in your other thread! :mad:
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.