c++ - I don't understand the object mismatch in a multithreading game -


i working on multithread game. has 3 classes :

  • carte, map of composed of several case
  • batiment, ship, ships fighting each others.
  • case, basic cell of map, , may linked batiment.

each thread control object of batiment, seems work great, when try output game, showing id numbers of differents threads representing batiment in map, doesn't work. instead of printing every thread, prints last batiment created in main.cpp.

i stagging 3 days on it...

here files :

main.cpp:

std::vector<batiment> batiments; for(int = 0; < nbrbatiments; i++){ //ajout des batiments sur le vecteur         //batiment temp = new batiment(&carte, i);         batiment temp(&carte, i);         batiments.push_back(temp);     }     for(int = 0; < nbrbatiments; i++){         if ((rc = pthread_create(&thread[i], null, batiment::launcher, (void *)&batiments[i]))) {             fprintf(stderr, "error: pthread_create, rc: %d\n", rc); return exit_failure;         }     } 

batiment.cpp: launcher function, use pass object thread :

void *batiment::launcher(void *context) {      return static_cast<batiment*>(context)->jouer(); } 

batiment.h: here members of class

carte *carteinside; static void *launcher(void *context); int id; void *jouer(); 

case.h: here member of class, linked batiment object :

    batiment *batiment; 

my batiment::jouer() method method move current object in map , shot object... when "cout" current id of object calls jouer(), described problem happens

carte.cpp:

for(int = 0; i<this->x; i++){     for(int j = 0; j<this->y; j++){         if(this->carte[i][j].libre == false){             printf("%d", this->carte[i][j].batiment->id);         }     } } 

carte.h: members of class

std::vector< std::vector<case> > carte; 

edit: constructor of batiment:

batiment::batiment(carte *carte, int i) {     this->carteinside = carte;     do{         this->x = dorand(0, carte->x);         this->y = dorand(0, carte->y);     }     while(!carteinside->carte[x][y].libre);     this->id = i;     this->orientation = dorand(0, 3);     this->longueur = dorand(2, 4);       this->vie = 2*(this->longueur)*(this->longueur);         carte->carte[this->x][this->y].libre = false;     carte->carte[this->x][this->y].batiment = this; } 

the problem populate carte via constructor of batiment, using temporary object destroyed making pointer invalid:

why ?

in statement create temporary batiment:

   batiment temp(&carte, i); 

your constructor apparently insert pointer newly created object in carte::carte[][].

unfortunately, define temporary in block of loop:

for(int = 0; < nbrbatiments; i++){ //ajout des batiments sur le vecteur         batiment temp(&carte, i);   // <=== l'instance n'existe que le temps d'une itération !!          ...     } 

so temporary created , destroyed in every single iteration of loop. pointer invalid leave loop.

when refer later, it's undefined behaviour. it's piece of luck finds meaningful value @ all.

by way, when pushback temp in vector, copy of object made (same values, still address).

how solve ?

well, keeping idea of inital design, there several solutions.

the simplest 1 create batiment on free store:

 batiment *temp = new batiment (&carte, i);   

the point object needs deleted 1 day somewhere. either take care of in destructor of carte, or change vecotr keep pointer can later delete these objects.

the better approach use shared_ptr, take care of memory maangement.


Comments

Popular posts from this blog

node.js - Mongoose: Cast to ObjectId failed for value on newly created object after setting the value -

gradle error "Cannot convert the provided notation to a File or URI" -

python - NameError: name 'subprocess' is not defined -