multithreading - C++ Vector Size is Zero -


i trying create function renders in vector of displayobject objects (on thread). using sdl thread.

here displayobject.h:

class displayobject { protected:     int width;     int height;     int x;     int y;     sdl_texture* texture;     sdl_renderer* renderer;  public:     ~displayobject();     int getx();     void setx(int x);     int gety();     void sety(int y);     int getwidth();     void setwidth(int width);     int getheight();     void setheight(int height);     sdl_texture* gettexture();     sdl_renderer* getrenderer(); }; 

in graphics.h have these variables:

std::vector<displayobject> imgarr; sdl_thread* renderthread; static int renderloop(void* vectorpointer); 

this code in graphics constructor:

textlabel textlabel(graphics->getrenderer(), 300, 80, "hallo welt", 50,       color(255, 0, 255), "xenotron.ttf"); //textlabel inherits displayobject imgarr.push_back(textlabel); renderthread = sdl_createthread(graphics::renderloop, "renderloop", &imgarr); 

this render loop function:

int graphics::renderloop(void* param) {     int counter = 0;     bool rendering = true;     std::vector<displayobject>* imgarr = (std::vector<displayobject>*)param;      while (rendering)     {         cout << imgarr->size() << endl;          counter++;         if (counter > 600)         {             rendering = false;         }          sdl_delay(16);     }      return 0; } 

the problem prints 0's in console. why that? supposed write 1 since pushed on object it.

when insert textlabel std::vector<displayobject>, stored in vector not original textlabel object, displayobject copy-constructed textlabel. want create textlabels new, store pointers them, , call delete when no longer need them.

the best solution use boost::ptr_vector<displayobject> instead - automatically call delete when erase objects it. http://www.boost.org/doc/libs/1_57_0/libs/ptr_container/doc/ptr_container.html

if can't use boost, can use c++11, can use std::vector<std::unique_ptr<displayobject>>.


Comments

Popular posts from this blog

cakephp - simple blog with croogo -

How to group boxplot outliers in gnuplot -

bash - Performing variable substitution in a string -