Versanator
Versanator

Reputation: 21

How can i prevent memory leaks in my C++ program?

I'm working on a program that uses a double linked list implementation of a sorted queue. This file is the only file that a memory leak occurs.

Also, I am not allowed to edit the header.

I understand that to prevent memory leaks you must delete all objects created using new .

My problem is that if i place a delete at the end of my Enqueue(Message msg) function like so:

void PriorityQ::Enqueue(Message msg)
{
Priorities P = msg.GetPriority();
Node* Location = frontPtr;
Node* PrevLocation = frontPtr;
Node* NewNode = new Node;
NewNode->data = msg;




  if (IsFull())
    throw FullPQ();
  else if (count == 0)
    {
        NewNode->previousPtr = NULL;
        NewNode->nextPtr = NULL;
        count++;
        frontPtr = NewNode;
        rearPtr = NewNode;  
    }
    else
    {
        Priorities  LP = Location->data.GetPriority();          
        while(LP >= P)
        {
            if(Location == NULL)
                break;

            PrevLocation = Location;
            Location = Location->nextPtr;

            if(Location != NULL)
                LP = Location->data.GetPriority();

        }//end line 50 while

             if(Location == NULL)
                {
                    NewNode->previousPtr = PrevLocation;
                    NewNode->nextPtr = Location;
                    PrevLocation->nextPtr = NewNode;
                    rearPtr = NewNode;
                    count++;

                }
            else
                {
                    PrevLocation = Location->previousPtr;
                    NewNode->previousPtr = PrevLocation;
                    NewNode->nextPtr = Location;

                    if(PrevLocation == NULL)
                    {
                        Location->previousPtr = NewNode;
                        count++;    
                        frontPtr = NewNode;                         
                    }
                    else
                    {
                        PrevLocation->nextPtr = NewNode;
                        Location->previousPtr = NewNode;
                        count++;                                    
                    }
                }// end line 73 else

    } //end Line 48 else
delete NewNode;
}//end  Enqueue() Function

I get a segmentation error the next time the Enqueue(Message msg) function is called, and if i place the delete in my destructor:

PriorityQ::~PriorityQ()
{
 MakeEmpty();

 delete NewNode;

}

it gives me this error priorityq.cpp [Error] 'NewNode' was not declared in this scope

So my question is how can I prevent memory leaks in my code without deallocating my objects in queue immediately after creating them.

Here is the full file.

#include "priorityq.h"


PriorityQ::PriorityQ()
{
    frontPtr = NULL;
    rearPtr = NULL;
    count = 0;

}

PriorityQ::~PriorityQ()
{
    MakeEmpty();

  delete NewNode;

}

void PriorityQ::MakeEmpty()
{
    frontPtr = NULL;
    rearPtr = NULL;
    count = 0;
}

void PriorityQ::Enqueue(Message msg)
{
    Priorities P = msg.GetPriority();
    Node* Location = frontPtr;
    Node* PrevLocation = frontPtr;
    Node* NewNode = new Node;
    NewNode->data = msg;




      if (IsFull())
        throw FullPQ();
  else if (count == 0)
    {
        NewNode->previousPtr = NULL;
        NewNode->nextPtr = NULL;
        count++;
        frontPtr = NewNode;
        rearPtr = NewNode;  
    }
    else
    {
        Priorities  LP = Location->data.GetPriority();          
        while(LP >= P)
        {
            if(Location == NULL)
                break;

            PrevLocation = Location;
            Location = Location->nextPtr;

            if(Location != NULL)
                LP = Location->data.GetPriority();

        }//end line 50 while

             if(Location == NULL)
                {
                    NewNode->previousPtr = PrevLocation;
                    NewNode->nextPtr = Location;
                    PrevLocation->nextPtr = NewNode;
                    rearPtr = NewNode;
                    count++;

                }
            else
                {
                    PrevLocation = Location->previousPtr;
                    NewNode->previousPtr = PrevLocation;
                    NewNode->nextPtr = Location;

                    if(PrevLocation == NULL)
                    {
                        Location->previousPtr = NewNode;
                        count++;    
                        frontPtr = NewNode;                         
                    }
                    else
                    {
                        PrevLocation->nextPtr = NewNode;
                        Location->previousPtr = NewNode;
                        count++;                                    
                    }
                }// end line 73 else

    } //end Line 48 else
delete NewNode;
}//end line 27 Enqueue() Function

void PriorityQ::Dequeue()
{
    if(IsEmpty())
    {
        EmptyPQ Empty;
        throw Empty;
    }

Node* Location = frontPtr;
frontPtr = frontPtr->nextPtr;
Location->nextPtr = NULL;

if(frontPtr != NULL)
{
    frontPtr->previousPtr = NULL;
}
else 
{
    MakeEmpty();
}
if(count != 0)
    count--;

}

void PriorityQ::Purge(Priorities p)
{
if(IsEmpty())
{
    EmptyPQ Empty;
    throw Empty;
}

Node* PurgePtr = frontPtr;
Priorities c = PurgePtr->data.GetPriority();
for(int j = 1; j < count; j++)
{
    if(c == p)
    {
        if(PurgePtr->previousPtr == NULL)
            Dequeue();
        else if( PurgePtr->nextPtr == NULL)
            PurgePtr->previousPtr->nextPtr = NULL;  
        else
        {
            PurgePtr->previousPtr->nextPtr = PurgePtr->nextPtr;
            PurgePtr->nextPtr->previousPtr = PurgePtr->previousPtr;
        }
    }// end line 130 if
    else
    {
        PurgePtr = PurgePtr->nextPtr;
        c = PurgePtr->data.GetPriority();

    }
}// end line 127 for

}

Message PriorityQ::Front() const
{
    if(IsEmpty())
        throw EmptyPQ();
    return frontPtr->data;
}

Message PriorityQ::Rear() const
{
    if(IsEmpty())
        throw EmptyPQ();
    return rearPtr->data;
}

Message PriorityQ::Peek(int n) const
{
    if(n <= (count - 1) )
    {
        Node* PeekPtr = frontPtr;

        for(int j = 0; j < n; j++)
        {
            PeekPtr = PeekPtr->nextPtr;
        }

        return PeekPtr->data;

    }
    else
        throw InvalidPeekPQ();


}

bool PriorityQ::IsFull() const
{
    if(count < 501)
        return false;
    else
        return true;
}

bool PriorityQ::IsEmpty() const
{
    if(count == 0 && frontPtr == NULL)
        return true;
    else
        return false;
}

int PriorityQ::Size() const
{

    return count;
}

Upvotes: 1

Views: 96

Answers (1)

B026
B026

Reputation: 63

The problem is NewNode is a pointer local of the method void Priority::Enqueue. So it can't be accessed from other method.

When the method finish NewNode is eliminate and you can't access it.

Upvotes: 1

Related Questions