C++ std::vector of pointers deletion and segmentation faults

20,416

Solution 1

Slight modified version compared to (@1800 INFORMATION).

  struct DeleteFromVector
    {
        template <class T>
        void operator() ( T* ptr) const
        {
            delete ptr;
        }
    };


std::for_each(aVec.begin(), aVec.end(), DeleteFromVector());

Solution 2

I don't know why you are crashing, but I guess that one possibility is that the size of the vector is not the same as the size you are passing in. Also I notice you are iterating from 0 to size-2, do you not mean to go all the way to the end?

One way to delete all of the items in the array using idiomatic C++ is something like this:

template<class T>
class deleter
{
  public:
    void operator()(const T* it) const
    {
      delete it;
    }
};

std::for_each(a.begin(), a.end(), deleter<Chromosome>());

Solution 3

Boost lambda already has a functor for deleting sequences of pointers, by the way:

std::for_each(a.begin(), a.end(), boost::lambda::delete_ptr());

Solution 4

I found the problem.

It was in the most well hidden (by none other than stupid old me) place it could be.

As some might have guessed this is a genetic algorithms program. It is for a tutorial I am making. I was choosing the crossover points for the chromosomes randomly from a roulette wheel function which I made. Well ... inside there, there was a -1 which should not be there. That destroyed literally everything, and eventually lead to a segmentation fault.

Thank you all for your help, I saw some really good practises in this post which I intend to follow

Solution 5

Are you sure that each pointer in the vector points to a different object? (i.e. that two pointers don't both point to the same object, which you're trying to delete twice.

Are you sure that you don't delete some of the pointers before calling this method? (i.e. are you sure that each pointer in the list points to a valid object?)

Share:
20,416
Lefteris
Author by

Lefteris

Software developer currently located in Berlin, Germany. Loves C, OpenGL and building things from the bottom up. Graduate of the Engineering Department of the university of Tokyo, Japan.

Updated on July 05, 2022

Comments

  • Lefteris
    Lefteris almost 2 years

    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().