Results 1 to 2 of 2

Thread: Who deletes the Worker object moved to a QThread?

  1. #1
    Join Date
    Apr 2014
    Qt products
    Qt3 Qt4 Qt5

    Default Who deletes the Worker object moved to a QThread?

    I need to give a little background in order to make my question understandable. I have an GUI application that uses several threads that can handle event processing. I've spent weeks looking over various articles on QT Threading and decided to use the Worker Object model presented in the official documentation (QT5.5.1 Assistant). My only code deviation is that I do not emit a finished signal from my Worker Object, so I can not create a connection to deleteLater(). Note: My Worker Object never has a finished state; the Worker Object must be viable until the application is terminated.

    Upon application termination, I need to delete the Work Object -- and currently I do so in the main thread. While this seems to work, the documentation I've read states the following:

    Calling delete on a QObject from a thread other than the one that owns the object (or accessing the object in other ways) is unsafe, unless you guarantee that the object isn't processing events at that moment. Use QObject::deleteLater() instead, and a DeferredDelete event will be posted, which the event loop of the object's thread will eventually pick up. By default, the thread that owns a QObject is the thread that creates the QObject, but not after QObject::moveToThread() has been called.
    That last sentence says that after a call to moveToThread, the owner of the Worker Object is no longer the thread that created the Worker Object (obviously). However, the documentation doesn't say whether or not the thread that receives the Worker Object becomes it's parent. If it did become the parent, then I would expect it to delete the Worker Object (just like every QObject with a parent does).

    If it doesn't become the parent, then how could the Worker Object get deleted except through its pointer in the thread of origin (or deleter(), which I cant exactly use in my situation)?

    I guess part of my confusion is the difference between being the owner and being the parent -- which I thought were synonymous in QT.

  2. #2
    Join Date
    Jan 2008
    Alameda, CA, USA
    Thanked 773 Times in 763 Posts
    Qt products
    Windows Android

    Default Re: Who deletes the Worker object moved to a QThread?

    I can't find anything definitive either. The best I can suggest is to put a breakpoint in the Worker Object's destructor and see if it hits, then examine the call stack to find out who is deleting it.

    I guess part of my confusion is the difference between being the owner and being the parent -- which I thought were synonymous in Qt.
    Well, not exactly. Being an owner means you control the lifetime of the C++ object instances you own, so you determine when they go out of scope and get deleted. If you are owned, you may not be able to find out who your owner is. Being parented is a more formal, hierarchical arrangement, eg. in Qt you can call QObject::parent() and get a pointer to your parent QObject (who is also your owner).

    An example: QTableWidgetItem instances are owned by the QTableWidget they are set on, but they are not parented by the QTableWidget. On the other hand, if you call QTableWidget::setCellWidget(), the QWidget pointer you pass in becomes both owned and parented by the QTableWidget.
    Last edited by d_stranz; 3rd May 2017 at 00:06.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. The following user says thank you to d_stranz for this useful post:

    SpiceWeasel (3rd May 2017)

Similar Threads

  1. QThread and New Object Creation
    By pratham_shah in forum Qt Programming
    Replies: 6
    Last Post: 26th April 2013, 12:11
  2. Replies: 6
    Last Post: 23rd March 2013, 22:50
  3. Replies: 15
    Last Post: 4th August 2012, 20:11
  4. Multiple start() on same QThread Object
    By mdecandia in forum Qt Programming
    Replies: 4
    Last Post: 3rd November 2009, 09:14
  5. strange ailment "Object moved to here"
    By s410i in forum Qt Programming
    Replies: 1
    Last Post: 29th August 2008, 00:27

Tags for this Thread


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.