Results 1 to 3 of 3

Thread: safe to delete object in other thread?

  1. #1
    Join Date
    Oct 2009
    Location
    Maryland
    Posts
    16
    Thanks
    3
    Thanked 3 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default safe to delete object in other thread?

    Is it safe to delete an object that is in a thread other than the current thread? For example:

    Qt Code:
    1. MyObject *m = new MyObject();
    2. // connect signal/slots, etc.
    3. m->moveToThread(otherThread);
    4.  
    5. // much later in same thread:
    6.  
    7. delete m;
    To copy to clipboard, switch view to plain text mode 

    The question is: Does the "otherThread" know the object is deleted? If not, this could this cause a problem with event handling in otherThread. This would not seem to be solved by "deleteLater" because there are two different threads of execution. Is it necessary to have a "selfDestruct()" method on the "MyObject" which calls deleteLater? If the thread uses a QPointer or QSharedPointer internally this would probably be OK.

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

    Default Re: safe to delete object in other thread?

    there are multiple factors to consider here. if you do 'new', and dont parent the QObject, then you are responsible for 'delete'ing it. You dont need to worry about signals slots when you delete a QObject, because they are disconnected automatically.

    Using deleteLater() should be ok.

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: safe to delete object in other thread?

    Quote Originally Posted by iraytrace View Post
    Is it safe to delete an object that is in a thread other than the current thread?
    No.

    The question is: Does the "otherThread" know the object is deleted?
    If you are unfortunate enough to delete the object while one of its methods is running, it will surely realize that when the OS kills the process

    This would not seem to be solved by "deleteLater" because there are two different threads of execution.
    Using deleteLater() should be safe, at least if you call it as a slot.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. thread-safe
    By babymonsta in forum Qt Programming
    Replies: 0
    Last Post: 5th May 2010, 10:18
  2. Why is QMutex Thread Safe?
    By Kind Lad in forum Newbie
    Replies: 3
    Last Post: 22nd February 2010, 04:46
  3. About the QSound,is thread safe?
    By cspp in forum Qt Programming
    Replies: 0
    Last Post: 6th November 2009, 14:26
  4. What makes something not thread safe?
    By tgreaves in forum Newbie
    Replies: 9
    Last Post: 20th February 2009, 20:16
  5. Replies: 4
    Last Post: 19th February 2009, 11:10

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.