c++ - Test class destructor for pointer being allocated? -


so having problem code. want pass value array of pointers function original object not 'disturbed' (my code works fine if pass reference; just trying different way learning exercise). after implementation returns, error:

"error object 0x100105790: pointer being freed not allocated *** set breakpoint in malloc_error_break debug".

i know because value goes out of scope upon function's return, destructor called object destructor assumes pointer allocated, error. curious if there way test if genome allocated or not. if not else destructor? problem worth bothering since have working passing in reference? function not destructive; have desire tricks. don't taking easy way out.

//class destructor int genome[] organism::~organism() {     //cout << "deleting this: " << << endl;     if (this->genome != null) {          delete [] this->genome;     } }  //declare genome pointer int *genome; /**  * default constructor class  */ organism::organism() {     this->fitness = 0;     this->size = 0;     this->genome = null; } //another constructor if size of genome defined organism::organism(int size) {     this->fitness = 0;     this->size = size;     this->genome = new int[size]; } //another constructor when starting values defined organism::organism(int size, int *genome) {     this->fitness = 0;     this->size = size;     this->genome = new int[size];      (int = 0; < size; i++) {         this->genome[i] = genome[i];     }  }  //initialize , populate reproducible existing array start_pop     (this has been verified being allocated , values initiated) vector<organism*> reproduceable (0);     (int = 0; < start_pop.size(); i++) {         if (start_pop[i]->get_fitness() > threshold) {             reproduceable.push_back(start_pop[i]);         }     }  //function definition organism* reproduce(organism, organism);  //function call in main() offspring.push_back(reproduce(*reproduceable[i], *reproduceable[i+1]));  //function implementation organism* reproduce(organism a, organism b) {     int genome[4];      //randomly decide on split parent genomes     int split = rand() % 5;     (int = 0; < a.get_size(); i++) {         if (i < split) {             genome[i] = a.get_genome()[i];         } else {             genome[i] = b.get_genome()[i];         }     }     //now cause random mutation in 2% of population     if ((rand() % 100 + 1) <= 2) {         int rand_index = rand() % 5;         int mutation = rand() % 6 + 1;          genome[rand_index] = mutation;     }      organism *child = new organism(4, genome); //need add genome     return child; } 

edited add default constructors , array initialization reproducible

your problem in line:

offspring.push_back(reproduce(*reproduceable[i], *reproduceable[i+1])); 

note pass 2 reproduceables value function. function's signature is

organism* reproduce(organism a, organism b) 

it expects 2 organism's value, means both organism's copy constructor called. since not define copy constructor, default 1 created, copies content of 1 organism another, including pointer go genome. issue both organisms freed -- 1 that's local function, , 1 passed it, resulting in double freeing memory allocated genome. there 2 ways solve it:

  1. make signature be
organism* reproduce(const organism &a, const organism &b) 

this way organism not copied, , not double freed. (based on question assume tried it, , interested in second solution :))

  1. implement custom copy constructor, creates new array, so
organism::organism(const organism& a) {      this->fitness = a.fitness;      this->size = a.size;      this->genome = new int[this->size];      (int = 0; < this->size; ++ i)          this->genome[i] = a.genome[i]; } 

this way pass organism function, custom copy constructor invoked, , genome organism local function allocated separately.


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -