Results 1 to 7 of 7

Thread: Who is going to destroy the object that is moved to thread?

  1. #1
    Join Date
    Mar 2013
    Posts
    28
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Windows

    Question Who is going to destroy the object that is moved to thread?

    Hi,

    I'm building a program with a working thread.

    Qt Code:
    1. Worker w1;
    2. Worker *w2 = new Worker;
    3.  
    4.  
    5. w1.moveToThread(t1);
    6. w2->moveToThread(t2);
    To copy to clipboard, switch view to plain text mode 

    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.

  2. #2
    Join Date
    Dec 2012
    Posts
    17
    Thanks
    12
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: Who is going to destroy the object that is moved to thread?


  3. #3
    Join Date
    Mar 2013
    Posts
    28
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Who is going to destroy the object that is moved to thread?

    Quote Originally Posted by alok9871 View Post
    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?

  4. #4
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Who is going to destroy the object that is moved to thread?

    - 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.
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  5. The following user says thank you to Santosh Reddy for this useful post:

    hind (25th March 2013)

  6. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Who is going to destroy the object that is moved to 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.

  7. #6
    Join Date
    Mar 2013
    Posts
    28
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Who is going to destroy the object that is moved to thread?

    - 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.

  8. #7
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Who is going to destroy the object that is moved to thread?

    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.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  9. The following user says thank you to amleto for this useful post:

    hind (25th March 2013)

Similar Threads

  1. Destroy object
    By Viper666 in forum Qt Programming
    Replies: 6
    Last Post: 5th January 2013, 14:43
  2. How to destroy an object right
    By Cruz in forum Newbie
    Replies: 7
    Last Post: 22nd January 2009, 19:43
  3. Object Not destroy
    By Sudhanshu Sharma in forum Qt Programming
    Replies: 3
    Last Post: 30th July 2008, 23:16
  4. How to destroy or terminate a thread ?
    By probine in forum Qt Programming
    Replies: 3
    Last Post: 26th December 2006, 16:44
  5. How to destroy a thread ?
    By probine in forum Qt Programming
    Replies: 1
    Last Post: 9th December 2006, 18:04

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.