Wednesday, 18 September 2013

How do I fix this memory leak in C++?

How do I fix this memory leak in C++?

I'm stuck on a program that is due for a class. I have to complete the
project with the correct output and without any memory leaks. Below is the
.h and .cpp for both LocationStack and LocationStackNode. I currently am
getting a memory leak on LocationStackNode and wondered if anyone could
help me with how I should write the destructor. Valgrind specifically
reports the memory leak when allocating new memory in
LocationStack::push(). I am also not allowed to modify the .h file at all.
stack-proj1.h:
class LocationStack {
public:
LocationStack(void);
~LocationStack();
void push(const Location &loc);
void pop(void);
const Location &getTop(void) const;
bool isEmpty(void) const;
bool isOn(const Location &loc) const;
friend ostream &operator<<(ostream &os, const LocationStack &s);
private:
const LocationStack &operator=(const LocationStack &)
{ assert(false); }
LocationStack(const LocationStack &) { assert(false); }
LocationStackNode *top;
};
class LocationStackNode {
public:
LocationStackNode(const Location &loc, LocationStackNode *next = NULL);
~LocationStackNode();
const Location &getLocation() const;
LocationStackNode *getNextNode() const;
void setNextNode(LocationStackNode *next);
private:
LocationStackNode() { assert(false); }
LocationStackNode(const LocationStackNode &) { assert(false); }
LocationStackNode &operator=(const LocationStackNode &)
{ assert(false); }
Location location;
LocationStackNode *nextNode;
};
stack-proj1.cpp
LocationStack::LocationStack(void){
top = NULL;
}
LocationStack::~LocationStack(){
delete top;
top = NULL;
}
void LocationStack::push(const Location &loc){
top = new LocationStackNode(loc, top);
}
void LocationStack::pop(void){
if(top->getNextNode()){
LocationStackNode *newNode;
newNode = top->getNextNode();
top = newNode;
}
else{
top = NULL;
}
}
const Location& LocationStack::getTop(void) const{
return top->getLocation();
}
bool LocationStack::isEmpty(void) const{
bool isEmpty = false;
if(!top){
isEmpty = true;
}
return isEmpty;
}
bool LocationStack::isOn(const Location &loc) const{
bool isOn = false;
int stackSize = 0;
if(!this->isEmpty()){
LocationStackNode *newNode = top;
LocationStackNode *newNode2 = top;
while(newNode2->getNextNode()){
newNode2 = newNode2->getNextNode();
stackSize++;
}
for(int n = 0; n <= stackSize; n++)/*while(newNode->getNextNode())*/{
if(newNode->getLocation() == loc){
isOn = true;
}
newNode = newNode->getNextNode();
}
}
return isOn;
}
ostream &operator<<(ostream &os, const LocationStack &s){
LocationStackNode *back = s.top;
LocationStackNode *next = s.top->getNextNode();
LocationStackNode *newNode = s.top;
newNode->setNextNode(NULL);
while(next){
newNode = next;
next = newNode->getNextNode();
newNode->setNextNode(back);
back = newNode;
}
while(newNode->getNextNode()){
os << newNode->getLocation() << endl;
newNode = newNode->getNextNode();
}
os << newNode->getLocation() << endl;
return os;
}
LocationStackNode::LocationStackNode(const Location &loc,
LocationStackNode *next){
location = loc;
nextNode = next;
}
LocationStackNode::~LocationStackNode(){
delete nextNode;
}
const Location& LocationStackNode::getLocation() const{
return location;
}
LocationStackNode* LocationStackNode::getNextNode() const{
return nextNode;
}
void LocationStackNode::setNextNode(LocationStackNode *next){
nextNode = next;
}
This is how Nodes are populated:
LocationStack stacks;
stacks.push(Location l); //Location is another class itself
Everything added to the stack uses the push() function of LocationStack.
Sorry there's so much code here. I've been trying to figure this out for a
while and I'm really not sure what the issue is. I just want to make sure
everything is here just in case there's another issue. If you're willing
to look through this for me, I will be so grateful. I just need to finish
this project so I can start on the next one. Thanks in advance for any
help!

No comments:

Post a Comment