c++ - How to remove memory leak in Qthreads? 12+Gb leak -
i'm trying speed (qt c++ opencv) program should count number of colors in photos future filtering. there no leak in single-threaded approach, slow.
with adding 8 threads i've speed process 5x times.
the problem starts when switch program multithreading.
there huge memory leak! http://snag.gy/chrrs.jpg
following advises (https://stackoverflow.com/a/12859444) prevented subclassing qthread , implementing run().
here loop counting each pixel in every new image shifted 1 pixel:
colorcountercontroller *cntrl[arrsize]; (int = 0; < box; i++)//x { (int j = 0; j < box; ++j)//y { cv::mat res=process(image,i,j); //using 1 core //colors=colordetectcontroller::getinstance()->colorscount(res); //using 8 cores cntrl[cnt2%arrsize]= new colorcountercontroller(res,this); ++cnt2; } ++cnt; emit setstatusprogresssignal((int)(cnt/amnt*100)); } delete[] *cntrl;
comments:
when using 1 core (above code) have singleton run colorscount(res) function. in case of 8 cores use same function called colorcountercontroller.
class colorcountercontroller : public qobject{ q_object private: qthread thread; colorcounter *colorcntr; pixalate *pixelate; private slots: void freecolorcntr(){ delete colorcntr; } public: colorcountercontroller(const cv::mat &image,pixalate *pxobj) { colorcntr= new colorcounter(); colorcntr->setimagethread(image); colorcntr->movetothread(&thread); connect(&thread, signal(started()), colorcntr, slot(colorscountthread())); connect(colorcntr, signal(finished()), &thread, slot(quit())); connect(colorcntr, signal(finished()), colorcntr, slot(deletelater())); connect(colorcntr, signal(results(int)), pxobj, slot(results(int))); thread.start(); } ~colorcountercontroller() { thread.quit(); thread.wait(); qdebug() << qstring("controller quit wait"); //delete colorcntr; //err }
i suppose leak in colorcountercontroller constructor:
colorcntr= new colorcounter();
but how avoid it? code causing error. in destructor:
//delete colorcntr; //err
and in constructor:
//connect(&thread, signal(finished()), &thread, slot(deletelater()));
please help!
p.s.
i changed this
delete[] *cntrl;
to this
(int = 0; < arrsize; i++){ if (cntrl[i]) delete cntrl[i]; }
and null pointers @ beginning before cntrl[cnt2%arrsize]
nothing changed
p.p.s. in case want contribute question: https://github.com/ivanesses/curiosity
2 problems cause leakage:
pointers new colorcountercontroller objects lost forever (and memory leaked) body of "for" loop run n times (n=box*box) creating n colorcountercontroller objects pointers 8 of them fit array later use delete objects.
cntrl array of pointers. need iterate through , call delete (plain delete, not delete[]) on each of it's elements.
as found op: must use imagethread.release(); instead of imagethread.deallocate()
Comments
Post a Comment