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. Confusion about QThread & deleteLater
Forum Updated to NodeBB v4.3 + New Features

Confusion about QThread & deleteLater

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 141 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.
  • K Offline
    K Offline
    KtrNozomi
    wrote last edited by
    #1

    I have a problem with how Qt handles objects destruction related to QThread.

    m_worker->moveToThread(m_thread);
    connect(m_thread, &QThread::finished, m_worker, &QObject::deleteLater);
    
    • m_thread belongs to the UI main thread, after moveToThread the m_worker thread affinity belongs to the sub thread. So the AUTO connection should be Qt::QueuedConnection which leads deleteLater to execute in the sub thread.
    • However, the thread has been finished and the thread event loop is stopped, deleteLater may never be executed. (I know deleteLater is asynchronous, maybe deleteLater is executed but the real "delete" may never be handled in the sub thread event loop?)

    It seems a paradox, what's the real problem here? I have read QObject::deleteLater here but still feel confused. Thanks a lot for answering!

    jsulmJ 1 Reply Last reply
    0
    • K KtrNozomi

      I have a problem with how Qt handles objects destruction related to QThread.

      m_worker->moveToThread(m_thread);
      connect(m_thread, &QThread::finished, m_worker, &QObject::deleteLater);
      
      • m_thread belongs to the UI main thread, after moveToThread the m_worker thread affinity belongs to the sub thread. So the AUTO connection should be Qt::QueuedConnection which leads deleteLater to execute in the sub thread.
      • However, the thread has been finished and the thread event loop is stopped, deleteLater may never be executed. (I know deleteLater is asynchronous, maybe deleteLater is executed but the real "delete" may never be handled in the sub thread event loop?)

      It seems a paradox, what's the real problem here? I have read QObject::deleteLater here but still feel confused. Thanks a lot for answering!

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote last edited by
      #2

      @KtrNozomi "If deleteLater() is called on an object that lives in a thread with no running event loop, the object will be destroyed when the thread finishes."
      https://doc.qt.io/qt-6/qobject.html#deleteLater

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      2
      • HansonH Offline
        HansonH Offline
        Hanson
        wrote last edited by
        #3

        @KtrNozomi "When this signal is emitted, the event loop has already stopped running. No more events will be processed in the thread, except for deferred deletion events. This signal can be connected to QObject::deleteLater(), to free objects in that thread."
        https://doc.qt.io/qt-6/qthread.html#finished

        “Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.”
        —— Martin Golding

        1 Reply Last reply
        3
        • K Offline
          K Offline
          KtrNozomi
          wrote last edited by
          #4

          @Hanson @jsulm Thanks a lot!

          1 Reply Last reply
          0
          • K KtrNozomi has marked this topic as solved

          • Login

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