如何防止C ++程序中的内存泄漏?

时间:2016-03-25 01:49:54

标签: c++ memory-leaks

我正在开发一个使用排序队列的双链表实现的程序。此文件是发生内存泄漏的唯一文件。

另外,我不允许编辑标题。

我了解为了防止内存泄漏,您必须删除使用new创建的所有对象。

我的问题是,如果我在delete函数的末尾添加Enqueue(Message msg),就像这样:

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

下次调用Enqueue(Message msg)函数时,如果我将delete放入析构函数中,则会出现细分错误:

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

 delete NewNode;

}

它给了我这个错误priorityq.cpp [Error] 'NewNode' was not declared in this scope

所以我的问题是如何在我的代码中防止内存泄漏而不在创建它们之后立即在队列中释放我的对象。

这是完整档案。

#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;
}

1 个答案:

答案 0 :(得分:1)

问题是NewNode是方法void Priority::Enqueue的本地指针。所以无法从其他方法访问它。

当方法完成NewNode被取消并且您无法访问它时。