Я работаю над программой, которая использует реализацию двойного связанного списка отсортированной очереди. Это единственный файл, в котором происходит утечка памяти.
Кроме того, мне не разрешено редактировать заголовок.
Я понимаю, что для предотвращения утечек памяти необходимо удалить все объекты, созданные с помощью 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;
}