c++ - class with Multithreaded member function -


i have class trying convert of member function run in different threads. while program complies without problem, crashes when trying read image buffer(which updated different thread). seems problem cause when argument passed incorrectly in _beginthread.

the following snippet of code should explain more trying do. trying accomplish have member function "fillbuffer" fill image buffer while rest of program doing else including reading same image buffer @ same time.

any syntax appreciated.

 const int maximgbuffersize = 5;         class myframe : public wxframe         {         public:             // constructors             myframe(const wxstring& title);             private:             static vector <iplimage*> imgbuffer;             void  changewp(wxcommandevent&);             void  fillbuffer();                 void  fillbufferfun();                 static void __cdecl getimgfrombuffer(void *);                 static void __cdecl pushimgbuffer(void *);         };      vector<iplimage*> myframe::imgbuffer;          enter code here      myframe::myframe(const wxstring& title)            : wxframe(...)     {        // stuff here        initializecriticalsection(&section);        fillbuffer();        // code here calls changewp(wxcommandevent&) time time     }      void myframe::fillbuffer()     {         while(imgbuffer.size() <= maximgbuffersize)         {             fillbufferfun();         }     }  void myframe::fillbufferfun() {    imgbuffer* img;    // img    _beginthread(pushimgbuffer, 0, img); }  void myframe::pushimgbuffer(void *p) {     entercriticalsection(&section);     imgbuffer.push_back( (iplimage*) p );     leavecriticalsection(&section); }  static unsigned int __stdcall getimgfrombuffer(void *);  void myframe::changewp(wxcommandevent&) {     // someting      iplimage* img = null;// new iplimage;         _beginthreadex( null, 0, myframe::getimgfrombuffer, img, 0, null );          // img         fillbuffer(); }  unsigned int myframe::getimgfrombuffer(void *p) {     entercriticalsection(&section);     p = (void *)imgbuffer[0];     imgbuffer.erase(imgbuffer.begin());     leavecriticalsection(&section);     return 0; } 

there couple of issues here:

  1. your code crashing because getimgfrombuffer function doesn't have effect seem intend it. seems, looking @ body of getimgfrombuffer, trying copy pointer value out of vector (myframe::imgbuffer) , use overwrite pointer value passed in function invoked (i.e., "img" variable inside myframe::changewp). "seems" because of initialization of "img" variable inside myframe::changewp (iplimage* img = new iplimage) before invoke getimgfrombuffer -- why assign new object pointer before assigning yet another, different pointer value (leading, of course, memory leak)? anyhow, assignment overwriting value of "p" argument inside getimgfrombuffer function, passed value , lost when function exits. if want getimgfrombuffer overwrite pointer variable passed in caller, need pass a pointer to pointer variable, so:
void myframe::changewp(wxcommandevent&) {     iplimage* img = null;  // no memory leak, time.     _beginthread(myframe::getimgfrombuffer, 0, & img);     //... }  void myframe::getimgfrombuffer(void ** p) {     //...     *p = (void *)imgbuffer[0];     //... } 
  1. i don't see how "section" defined, whether it's static or instance, it's @ least bad form either way. if it's static, you're making mistake of re-initializing every time construct. realize class representing top-level window, won't making more 1 of them, it's still bad form. if section instance variable, have multiple section objects trying protect single (static) resource, no mutual exclusion between them. again, there's 1 window, isn't actual problem, still...

there's more, enough start with.


Comments

Popular posts from this blog

c# - How to set Z index when using WPF DrawingContext? -

razor - Is this a bug in WebMatrix PageData? -

visual c++ - Using relative values in array sorting ( asm ) -