0

I have a vector of pointers of a class named Individual. In said class I have an int array and an int vector. I have tried three different approaches to clear the vector based on answers I've seen here on stackoverflow but none seem to work. Here are they:

Method 1:

int populationSize = (int) this->population.size();
this->population.clear();
this->population.shrink_to_fit();
this->population = *new vector<Individual*>(populationSize, NULL);

Method 2:

int populationSize = (int) this->population.size();
vector<Individual*>(this->population).swap(this->population);
this->population = *new vector<Individual*>(populationSize, NULL);

Method 3:

int populationSize = (int) this->population.size();
for (std::vector< Individual* >::iterator it = this->population.begin() ; it != this->population.end(); ++it)
    {
        delete *it;
    }
this->population.clear();
this->population = *new vector<Individual*>(populationSize, NULL);

None of those seems to work and I really need to get this code going. Thanks in advance for help.

Ps: Here are the attributes and the destructor method of the Individual class, although I'm pretty sure that's not the problem.

//Attributes
int fitness, limit, *sudokuBoard;
vector<int> fixedPositions;
static Helper h;
//Destructor
~Individual(void) {delete [] this->sudokuBoard; this->fixedPositions.clear();};

EDIT: It's not a duplicate question since the information provided does not answer mine.

Yan Mendes
  • 25
  • 6
  • 1
    The evaluation of the subexpression: `*new vector(populationSize, NULL);` - is a leak. BTW, creating a `std::vector` using `new` is almost never a good sign. Also avoid C styled cast... – WhiZTiM Jun 06 '17 at 21:06
  • @WhiZTiM swapped the code you told me for this->population.resize(populationSize); since all I needed was to reset the vector size, but the leak is not there. – Yan Mendes Jun 06 '17 at 21:18
  • This might be a dumb question, but how are you able to know you have a memory leak in the first place? – Cameron Jun 06 '17 at 22:02
  • @Wild dog I'm monitoring the process with htop – Yan Mendes Jun 06 '17 at 22:51
  • you might come from java or C#. In C++, explicit new is never a good idea. – The Techel Jun 07 '17 at 08:59

0 Answers0