Results 1 to 3 of 3

Thread: QObject::destroy() - ensure that the destruction is done before processing the signal

  1. #1
    Join Date
    Apr 2011
    Posts
    14
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QObject::destroy() - ensure that the destruction is done before processing the signal

    Hello There,

    My question is pretty simple:
    How is it possible to ensure that an actual QObject is destroyed? Connecting to QObject::destroyed() does not guarantee that the destruction is already done and I could not find any call anywhere to wait until it is done (I was thinking on QCoreApplication:rocessEvents() or QObject::sender() set to 0 but none of those work).
    So, how to do that?


    (If interested why:
    In practice the sender object is a QThread what calls deleteLater() and quit() when it is finished. The slot connected to its destroy() signal - what is emitted by Qt just after the ~ destructor runs - sometimes invoked earlier than the destruction of the object (QObject/QThread or their subclasses) is actually done. It is clear since for example if a QCoreApplication::quit() is called from the slot then it sometimes (sometimes not) results in a "QThread: Destroyed while thread is still running". That also makes it quite difficult to reuse certain pointers.)

    Cheers

  2. #2
    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: QObject::destroy() - ensure that the destruction is done before processing the si

    A QObject emits QObject::destroyed(), as the docs say:
    This signal is emitted immediately before the object obj is destroyed, and can not be blocked.
    (emphasis mine). The object doesn't exist after the destructor is run, so it clearly cannot emit anything, and even if it could the sender() would be invalid... leading quickly to crash city.

    The problem you are trying to solve sounds like you want a way to ensure threads are stopped before exiting the application. Perhaps connect something to QCoreApplication::aboutToQuit() and do your thread cleanup there?

  3. #3
    Join Date
    Sep 2015
    Posts
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QObject::destroy() - ensure that the destruction is done before processing the si

    Hi,

    Set this attibute to your QWidget and then you can catch the signal destroyed() when close button is clicked:

    Qt Code:
    1. setAttribute(Qt::WA_DeleteOnClose);
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. QObject::connect: Connecting from COMPAT signal...
    By ricardo in forum Qt Programming
    Replies: 1
    Last Post: 29th June 2009, 17:09
  2. QObject signal/slot not working
    By Msnforum in forum Qt Programming
    Replies: 4
    Last Post: 24th January 2009, 22:50
  3. QObject::connect: No such signal
    By caseyong in forum Qt Programming
    Replies: 5
    Last Post: 19th February 2008, 07:23
  4. Replies: 1
    Last Post: 24th June 2006, 20:55

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.