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