Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6874263
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:10:21+00:00 2026-05-27T04:10:21+00:00

I am trying to implement a Pool Class that maintains a pool of linked

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T04:10:21+00:00Added an answer on May 27, 2026 at 4:10 am

    You allocate an array of employees:

    vPool = new tEmployee[MAX_POOL];
    

    and then incorrectly attempt to delete them individually:

    delete curr; // Don't do this
    

    before correctly deleting the array:

    delete [] vPool;
    

    As a general rule, each new must be matched with one delete; you didn’t new the employees individually, so don’t delete them individually.

    You’ll also need to maintain a list of pointers to all the arrays you allocate, so you can delete them all in the destructor; currently, you leak all of them except the last one you allocated. I would suggest something like:

    std::vector<tEmployee *> vPool; // store all allocated blocks
    
    tEmployee* GetFromPool() {
       if (vDeleted) {
           tEmployee * temp = vDeleted;
           vDeleted = vDeleted->next;
           return temp;
      }
    
      if (vTop == MAX_POOL) {    
        vPool.push_back(new tEmployee[MAX_POOL]); // add new block to collection
        vTop = 0;
      }
    
      return vPool.back() + vTop++;
    }
    
    ~Pool() {
        for (size_t i = 0; i < vPool.size(); ++i)
            delete vPool[i];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

SetFocus I'm trying implement the above Se Focus code in a Class Library that
I'm trying to implement a basic worker pool using pthreads. The scenario is that
I am trying to implement a thread pool that processes a task queue using
when trying to implement an Aspect, that is responsible for catching and logging a
I am trying to implement jdbc-pool in a standalone web app (self contained -
I'm trying to implement persistent database connection pool with django. One of the options
I'm trying to implement a sort of thread pool whereby I keep threads in
Trying to implement a MSMQ-backed WCF PubSub. I understand that net.msmq is one-way; however
I'm trying to implement a simple web server on Linux that connects to the
I am currently trying implement a QThread that contains a socket connection. The socket

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.