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 207425
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T17:44:04+00:00 2026-05-11T17:44:04+00:00

I have a vector of pointers to a class. I need to call their

  • 0

I have a vector of pointers to a class. I need to call their destructors and free their memory. Since they are vector of pointers vector.clear() does not do the job.So I went on to do it manually like so :

void Population::clearPool(std::vector<Chromosome*> a,int size)
{
    Chromosome* c;
    for(int j = 0 ;j < size-1;j++)
    {
       c = a.back();
       a.pop_back();
       delete c;
       printf("  %d \n\r",j);
       c = NULL;

    }

}

The printf in there is since I have a talking destructor to see in which Chromosome the segmentation fault happens. When clearPool() is called and say we got a size of 100, it can give a segmentation fault in any Chromosome between 0 and 100.

I have no idea why this might be happening nor do I have a way to actually find what’s wrong since while debugging with breakpoints all I see is that it happens in there at random chromosomes.

I am using codeblocks IDE and the gdb debugger. The stack trace when the segmentation fault happens has 4 memory addresses and a function wsncpy().

  • 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-11T17:44:04+00:00Added an answer on May 11, 2026 at 5:44 pm
    void Population::clearPool( std::vector <Chromosome*> & a )
    {
       for ( int i = 0; i < a.size(); i++ ) {
          delete a[i];
       }
       a.clear();
    }
    

    Notice that the vector is passed by reference. In your code, a copy of the vector is used, which means that it is unchanged in the calling program. Because you delete the pointers in the copy, the pointers in the original are now all invalid – I suspect you are using those invalid pointers in some way not shown in the code you posted.

    As a couple of template solutions have been posted that use C++ library algorithms, you might also want to consider a template solution that does not:

    template <class C> void FreeClear( C & cntr ) {
        for ( typename C::iterator it = cntr.begin(); 
                  it != cntr.end(); ++it ) {
            delete * it;
        }
        cntr.clear();
    }
    

    Using this you can free any container of dynamically allocated objects:

    vector <Chromosome *> vc;
    list <Chromosome *> lc;
    // populate & use
    FreeClear( lc );
    FreeClear( vc );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a vector of pointers to class objects. These class objects invoke new
I have a class with a vector of pointers to objects. I've introduced some
I have a vector of pointers. I would like to call a function for
Need help... I have 3 classes, Manager which holds 2 pointers. One to class
I have a class that has a vector of objects. What do I need
I have a set of objects in a vector of pointers to their baseclass
I have a vector with pointers of type Vehicle. Vehicle is the base class
I have a vector of pointers to objects. I'd like to remove objects from
I have a vector of pointers to Mouse objects called 'mice'. I'm passing the
I have a vector that holds pointers to abstract type Rock : vector<Rock*> rocks;

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.