PDA

View Full Version : Who is going to destroy the object that is moved to thread?



hind
21st March 2013, 03:05
Hi,

I'm building a program with a working thread.


Worker w1;
Worker *w2 = new Worker;

QThread t1;
QThread t2;

w1.moveToThread(t1);
w2->moveToThread(t2);

But who is going to delete w1 and w2 after t1 and t2 are stopped?
I think the thread where w1 and w2 is created is better not doing that. But when t1 and t2 are stopped, the working threads don't exist any more, it's not likely to delete them after that. I found that with t1 stopped and having its threadID back to 0, w1's threadID is still the one t1 used to have, which may imply that w1 is not going to any new thread after t1 stopped. Especially for w1, it can only be destroyed by the object inside which it is declared, which lives in the old thread. For w2, I have no idea where and when to call delete w2.

Maybe I have some misunderstanding with this, any help will be appreciated.

alok9871
21st March 2013, 04:51
This might help you

http://mayaposch.wordpress.com/2011/11/01/how-to-really-truly-use-qthreads-the-full-explanation/

hind
21st March 2013, 06:23
This might help you

http://mayaposch.wordpress.com/2011/11/01/how-to-really-truly-use-qthreads-the-full-explanation/

Thanks for that. It helps with the using of deletelater(). But my questions is still not answered yet:(. Where is the destructor of worker object called? In which function and which thread?

Santosh Reddy
21st March 2013, 10:21
- Object w1 will be deleted / destroyed automatically when it goes out of scope.
- Moving w1 to another thread is very bad idea; it may potentially crash the program.
- w2 has to be explicitly deleted inside thread t2.
- If you want the QThread to manage QObjects (like w2) then you better implement a derived QThread.

ChrisW67
21st March 2013, 23:39
- If you want the QThread to manage QObjects (like w2) then you better implement a derived QThread.
Follow the approach in the Qt5 QThread docs or the blog alok9871 linked to. Subclassing QThread is no longer encouraged.

hind
22nd March 2013, 04:59
- Object w1 will be deleted / destroyed automatically when it goes out of scope.
- Moving w1 to another thread is very bad idea; it may potentially crash the program.
- w2 has to be explicitly deleted inside thread t2.
Thank you very much! That's exactly what I was trying to find out. And, does that mean w2 must be deleted before t2 terminates? And we know the object t2 lives in the old thread so how do I make sure t2 deletes w2 in the new thread?


- If you want the QThread to manage QObjects (like w2) then you better implement a derived QThread.

Follow the approach in the Qt5 QThread docs or the blog alok9871 linked to. Subclassing QThread is no longer encouraged.
Yes it seems that I will have to subclass QThread to manually delete w2. But the "moveToThread" way looks against the "no subclassing" way while they are both encouraged now:(. BTW I'm working on 4.8.4.

amleto
23rd March 2013, 22:50
just make w2 a qobject and set its parent to the new thread. the new thread will delete the worker when the thread is destroyed. Or just use deleteLater(). There is still no need to subclass QThread.