Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Why deleteLater can be connected with finished signal?
Forum Updated to NodeBB v4.3 + New Features

Why deleteLater can be connected with finished signal?

Scheduled Pinned Locked Moved General and Desktop
9 Posts 3 Posters 7.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Z Offline
    Z Offline
    zhou13
    wrote on last edited by
    #1

    I have saw that many people recommend use the follow code to delete Qthread itself.

    @QObject::connect(this,SIGNAL(finished()),this,SLOT(deleteLater()));@

    I have two question:

    1. Why this code is right? When the main loop of the main thread try to delete QThread, this thead may not even end since it still emited a signal before a short time. Does finished main I can delete it?
    2. If this is right, can we just write

    @QObject::connect(this,SIGNAL(finished()),this,SLOT(deleteRIGHTNOW()));@

    if we have deleteRIGHTNOW slot?

    1 Reply Last reply
    0
    • T Offline
      T Offline
      twsimpson
      wrote on last edited by
      #2

      "QObject::deleteLater()":http://qt-project.org/doc/qt-4.8/qobject.html#deleteLater adds an event to the event queue that will, in turn, delete the object. Remember that a QThread is not a thread, it simply represents control of a thread, when a thread is "finished" it has completed and so ended execution.

      1 Reply Last reply
      0
      • Z Offline
        Z Offline
        zhou13
        wrote on last edited by
        #3

        That does not answer both of the question……

        For the first question, does a Qthread finished means its thread terminated? I don't think so.

        1 Reply Last reply
        0
        • T Offline
          T Offline
          twsimpson
          wrote on last edited by
          #4

          The method QThread::run() is what's run in the new thread, once that method returns (when the event-loop ends) the thread ends execution. The thread isn't terminated, but it has ended.

          1 Reply Last reply
          0
          • Z Offline
            Z Offline
            zhou13
            wrote on last edited by
            #5

            But if the thread really ends, how qt can emit the signal?

            1 Reply Last reply
            0
            • G Offline
              G Offline
              giesbert
              wrote on last edited by
              #6

              The finished signal of a thread is emitted, when the execution ended, so the attached thread is not alive anymore. deleteLater is executed asynchronously in the event loop of the creating thread of the object, which is typically the main thread. Using this technique you can delete a thread object safely.

              Nokia Certified Qt Specialist.
              Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

              1 Reply Last reply
              0
              • G Offline
                G Offline
                giesbert
                wrote on last edited by
                #7

                [quote author="zhou13" date="1346654213"]But if the thread really ends, how qt can emit the signal?[/quote]

                Only the worker threadc ends, there is some magic code before and after execution of the thread (see code of QThread) which does the needed stuff.

                Nokia Certified Qt Specialist.
                Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

                1 Reply Last reply
                0
                • Z Offline
                  Z Offline
                  zhou13
                  wrote on last edited by
                  #8

                  It isn't convenient for me to see the code of QT.

                  So QThread uses more than one thread?

                  Also @QObject::connect(this,SIGNAL(finished()),this,SLOT(deleteRIGHTNOW()));@
                  is also safe since this connection is not only queued, but also do after finished?

                  1 Reply Last reply
                  0
                  • G Offline
                    G Offline
                    giesbert
                    wrote on last edited by
                    #9

                    Sorry, I did not get the last question.

                    why isn't it convenient for you to look at Qt code? It explains all, and it implements all. Run is part opf the thread, there is some setup code before and some clean up code after QThread::run. This code emits the finished signal just before leaving the thread.

                    QThread does not use threads. QThread is the handler class for exactly one thread, not more, not less.

                    what do you mean by the connect question?

                    edit: that code might bring up a crash, as it deletes immediately and not waits for the next run of the event loop. If the connection is a queued connection and the receiving object does not live in the thread, it might work. But why create new things if working things exist? If deleteRIGHTNOW is done via a Queued connection, it could work, if it is done directly, it will crash.

                    Nokia Certified Qt Specialist.
                    Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

                    1 Reply Last reply
                    0

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved