Results 1 to 8 of 8

Thread: QThread deleteLater()

  1. #1
    Join Date
    Jun 2009
    Posts
    66
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default QThread deleteLater()

    Hi,

    I've got an issue where I'm trying to use deleteLater() to delete a thread after it has finished running. Therefore I have:

    Qt Code:
    1. void MyThread::run()
    2. {
    3. //MyThread Work done here
    4.  
    5. deleteLater();
    6. }
    To copy to clipboard, switch view to plain text mode 
    However, since MyThread has no parent, the deletion of the thread seems to only occur when the applciation exits. Does anyone know how this can be changed?

    David

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    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: QThread deleteLater()

    deleteLater() has nothing to do with parentship. You just need a running event loop in the thread owning the object you wish to delete (in your case the QThread instance).

    However such an unconditional self-deletion is dangerous since you don't know whether the object has been allocated on heap or on stack. The object should never delete itself. It is much better to connect the finished() signal to the deleteLater() slot from outside of the object.
    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.


  3. #3
    Join Date
    Jun 2009
    Posts
    66
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QThread deleteLater()

    Can any qobject have an eventloop?

    I have a qobject that is a storage class called 'Card'. Because some of the cleardown routines for this class take time to complete, I run them in a thread that is a member of that Card object.

    Therefore once that thread has completed, i want the qthread itself to be destroyed.

    I also tried:

    connect( m_pCloseComponentsThread, SIGNAL( finished() ), m_pCloseComponentsThread, SLOT( deleteLater() ), Qt:irectConnection );

    Just after creating the thread, but this still fails to call the qthread destructor until my app exits.

    Any ideas?

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    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: QThread deleteLater()

    Quote Originally Posted by doggrant View Post
    Can any qobject have an eventloop?
    Objects don't have event loop. Threads do.

    Because some of the cleardown routines for this class take time to complete, I run them in a thread that is a member of that Card object.
    That's probably not a very good idea.

    Just after creating the thread, but this still fails to call the qthread destructor until my app exits.
    Do you have an event loop running?
    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.


  5. #5
    Join Date
    Jun 2009
    Posts
    66
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QThread deleteLater()

    Because some of the cleardown routines for this class take time to complete, I run them in a thread that is a member of that Card object.
    That's probably not a very good idea.
    I know this might not be the best idea, but since i have no control over the external library, which is slow, I need to use a thread to call the external routines.

    What I have is the following:

    QMainWindow, which has an event loop (Have called exec() is here)
    |
    |_ _ _ Hotswap QThread that uses waitformultiple objects to wait for events from an external library.
    Exec() not called here, since my own thread processing is in run()
    |
    |_ _ _ When an event fires in the hotswap thread, a card object is created, and
    after getting info about the card, the cleanup thread is run.
    Note: The card object will still be alive after the cleanup thread has been executed.


    So what I really need is a way to call delete on the cleanup thread, when the finished() signal fires, but i'm not sure how to do this in this instance.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    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: QThread deleteLater()

    Quote Originally Posted by doggrant View Post
    I know this might not be the best idea, but since i have no control over the external library, which is slow, I need to use a thread to call the external routines.
    No, you don't. Unless you want to crash your app sooner or later but there are easier ways to do this than allowing multiple threads to access the same data.

    So what I really need is a way to call delete on the cleanup thread, when the finished() signal fires, but i'm not sure how to do this in this instance.
    Make the QThread object controlling the cleanup thread live in the main thread so that its slot gets executed.
    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.


  7. #7
    Join Date
    Jun 2009
    Posts
    66
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QThread deleteLater()

    Hi,

    when looking at the stack there was nothing there, the thread had exited, unless i should have been looking at the stacks of other threads involved.

    On a good(ish) note, i tried the same application today, and the problem does not occur when debugging. I beleive it may be a timing issue when debugging, but will not be able to look at it again unless it re-occurs!

    David

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

    Default Re: QThread deleteLater()

    The QThread should be on the main thread anyway - you're not moving it anywhere, are you?
    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.

Similar Threads

  1. Let QThread delete itself with deleteLater();
    By Lodorot in forum Qt Programming
    Replies: 1
    Last Post: 24th February 2010, 16:48
  2. To use or not to use: deleteLater()
    By codeslicer in forum Qt Programming
    Replies: 11
    Last Post: 11th July 2009, 21:43
  3. QT deleteLater again.
    By bunjee in forum Qt Programming
    Replies: 1
    Last Post: 10th May 2008, 22:36
  4. Qt Deletelater
    By bunjee in forum Qt Programming
    Replies: 2
    Last Post: 8th January 2008, 16:58
  5. DeleteLater works... but why?
    By TheGrimace in forum Qt Programming
    Replies: 11
    Last Post: 6th June 2007, 15:14

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.