I am trying to implement a Pool Class that maintains a pool of linked list nodes
Although allocation and deallocation are working correctly, destructor is throwing an exception.
class Pool {
public:
Pool ();
~Pool ();
tEmployee *GetFromPool (void);
void GiveToPool (tEmployee * pNode);
void PrintPoolSize ();
private:
int vTop;
tEmployee *vPool;
tEmployee *vDeleted;
};
Here are the implementation of functions
Pool::Pool ()
:vTop (0), vDeleted (NULL)
{
vPool = new tEmployee[MAX_POOL];
}
tEmployee* Pool::GetFromPool (void)
{
if (vDeleted) {
tEmployee * temp = vDeleted;
vDeleted = vDeleted->next;
return temp;
}
if (vTop == MAX_POOL) {
vPool = new tEmployee[MAX_POOL];
vTop = 0;
}
return vPool + vTop++;
}
void Pool::GiveToPool (tEmployee * pNode)
{
pNode->next = vDeleted;
vDeleted = pNode;
}
Pool::~Pool ()
{
tEmployee *curr = vDeleted;
tEmployee *next = 0;
while (curr) {
next = curr->next;
delete curr; //This line is throwing exception on the second iteration of the loop
curr = next;
}
delete [] vPool;
}
Is it due to heap corruption?