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. When to delete QThread
Forum Updated to NodeBB v4.3 + New Features

When to delete QThread

Scheduled Pinned Locked Moved General and Desktop
11 Posts 5 Posters 16.4k 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.
  • R Offline
    R Offline
    rovshanb
    wrote on last edited by
    #1

    Hi,

    I have following code in tree model derived from QAbstractItemModel:

    @void DbTreeModel::populateChildNodes(const QModelIndex & parent)
    {
    ... do some prosessing here ...

    nodePopulatorThread=new NodePopulatorThread(parent, this);
    connect(nodePopulatorThread, SIGNAL(queryCompleted(DbTreeItemResult)), this, SLOT(nodeListAvailable(DbTreeItemResult)));
    nodePopulatorThread->start();
    

    }@

    and nodeListAvailable slot looks like this:

    @void DbTreeModel::nodeListAvailable(const DbTreeItemResult &result)
    {
    nodePopulatorThread->wait();
    delete nodePopulatorThread;
    nodePopulatorThread=0;

    ... do processing of result ...
    

    }@

    My question is: Is it right to delete thread at the beginning of method, or should I delete it after processing results (because result parameter is created in thread and passed by reference to this slot)? Currently it works OK on Linux, but I think that it can lead to random crashes because data pointed by result parameter may be deleted (when deleting thread) by the time I start to process it. Or am I talking nonsense? ))

    Thanks in advance!

    1 Reply Last reply
    0
    • K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      There a difference in your code. I guess it should not run this way, because the parameter lists are different.

      [quote author="rovshanb" date="1310553943"]
      @ connect(nodePopulatorThread, SIGNAL(queryCompleted(DbTreeItemResult)), this, SLOT(nodeListAvailable(DbTreeItemResult)));
      nodePopulatorThread->start();
      }@
      [/quote]
      and here you have a const reference
      [quote author="rovshanb" date="1310553943"]
      @
      void DbTreeModel::nodeListAvailable(const DbTreeItemResult &result) @
      [/quote]

      Otherwise you may adapt the parameter list of your slot method. if you are not using a const reference but a copy it should not be a problem.

      If you adapt your connect statement to reflect the use as in your slot method, you are correct about worrying about the life time of your result.

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      0
      • C Offline
        C Offline
        changsheng230
        wrote on last edited by
        #3

        Better not delete thread directly, it is very dangerous, use QObject::deleteLater(), which will post an event that will ultimately cause its deletion by the thread the object is living in.

        There is a best practice wiki talking about details of good-to-use thread, of course deletion included:
        http://developer.qt.nokia.com/wiki/Threads_Events_QObjects

        Chang Sheng
        常升

        1 Reply Last reply
        0
        • R Offline
          R Offline
          rovshanb
          wrote on last edited by
          #4

          I changed connect method to:

          @connect(nodePopulatorThread, SIGNAL(queryCompleted(const DbTreeItemResult &)), this, SLOT(nodeListAvailable(const DbTreeItemResult &)));@

          and moved delete statements to end of method.

          But the slot was also firing with the above code (Qt Creator auto suggested it).

          So when I write @SLOT(nodeListAvailable(DbTreeItemResult))@ and define slot as @DbTreeModel::nodeListAvailable(const DbTreeItemResult &result)@ does it work differently than when specifying const & both in connect and slot?

          1 Reply Last reply
          0
          • R Offline
            R Offline
            rovshanb
            wrote on last edited by
            #5

            Ok, thanks for the link, I'll read it now

            1 Reply Last reply
            0
            • K Offline
              K Offline
              koahnig
              wrote on last edited by
              #6

              [quote author="rovshanb" date="1310554925"]I changed connect method to:

              @connect(nodePopulatorThread, SIGNAL(queryCompleted(const DbTreeItemResult &)), this, SLOT(nodeListAvailable(const DbTreeItemResult &)));@

              and moved delete statements to end of method.

              But the slot was also firing with the above code (Qt Creator auto suggested it).

              So when I write @SLOT(nodeListAvailable(DbTreeItemResult))@ and define slot as @DbTreeModel::nodeListAvailable(const DbTreeItemResult &result)@ does it work differently than when specifying const & both in connect and slot?[/quote]

              I do not know. So far I believed that this will result in an error while connecting. Actually it did always give an error message to the console then and returned a false. However, could be just a conclusion on my side. When I did not receive a signal in a slot it was quite often a mismatch of the parameter lists.

              Vote the answer(s) that helped you to solve your issue(s)

              1 Reply Last reply
              0
              • R Offline
                R Offline
                rovshanb
                wrote on last edited by
                #7

                Ok, thank you very much for helping!

                1 Reply Last reply
                0
                • D Offline
                  D Offline
                  dakron
                  wrote on last edited by
                  #8

                  You can also make your result item class as implicitly shared object "http://doc.qt.nokia.com/4.7-snapshot/implicit-sharing.html":http://doc.qt.nokia.com/4.7-snapshot/implicit-sharing.html and then you can be sure that you don't 'lose' your results.

                  1 Reply Last reply
                  0
                  • K Offline
                    K Offline
                    koahnig
                    wrote on last edited by
                    #9

                    In case someone has similar doubts as expressed above. There has been another thread started "Signals/Slots behavior review":http://developer.qt.nokia.com/forums/viewthread/7771/ detailing the behaviour and the reasoning behind it.

                    [quote author="rovshanb" date="1310554925"]
                    But the slot was also firing with the above code (Qt Creator auto suggested it).
                    [/quote]

                    When the auto suggestions are changing parameter lists may be consiedered as a styling issue then.

                    Vote the answer(s) that helped you to solve your issue(s)

                    1 Reply Last reply
                    0
                    • L Offline
                      L Offline
                      luca
                      wrote on last edited by
                      #10

                      Hi all,
                      I post my question in this thread to avoid a creation of a new topic.

                      I have a QObject class:
                      @
                      class GestoreComunicazioneSatellite : public QObject
                      {
                      Q_OBJECT
                      public:
                      ...
                      ...
                      }

                      GestoreComunicazioneSatellite::GestoreComunicazioneSatellite() :
                      QObject(0)
                      {
                      ...
                      m_thread=new QThread();
                      this->moveToThread(m_thread);
                      ...
                      }
                      @

                      The problem is that I get some warning when I destroy the GestoreComunicazioneSatellite object.

                      I tryed some solution:
                      @
                      GestoreComunicazioneSatellite::~GestoreComunicazioneSatellite()
                      {
                      m_thread->deleteLater();
                      }
                      @
                      I get:
                      Warning: --> QThread: Destroyed while thread is still running

                      @
                      GestoreComunicazioneSatellite::~GestoreComunicazioneSatellite()
                      {
                      m_thread->terminate();
                      m_thread->wait();
                      m_thread->deleteLater();
                      }
                      @
                      I get:
                      Warning: --> QThread::wait: Thread tried to wait on itself

                      The only way to avoid warnings is:
                      @
                      GestoreComunicazioneSatellite::~GestoreComunicazioneSatellite()
                      {
                      }
                      @

                      but I'm not sure the m_thread is destroyed this way.

                      What do you think about?

                      1 Reply Last reply
                      0
                      • L Offline
                        L Offline
                        luca
                        wrote on last edited by
                        #11

                        ... and what if I try:
                        @
                        GestoreComunicazioneSatellite::~GestoreComunicazioneSatellite()
                        {
                        m_thread->exit(123);
                        m_thread->deleteLater();
                        }
                        @

                        ?

                        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