Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
qtws26_logo

The Qt Event of the Year: Qt World Summit 2026

Save Your Spot >
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QThread::quit() does not call workerthread's destractor
Qt 6.12 LTS is out! See what's new in the release blog

QThread::quit() does not call workerthread's destractor

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 642 Views
  • 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.
  • Y Offline
    Y Offline
    ynakane
    wrote on last edited by
    #1

    Hi.
    I'm a beginner of Qt multithreaded programming.

    I wrote the multithread code with reference to the Qt reference.

    But I noticed something strange.
    Workerthread's destructor does not called after QThread :: quit () on main thread.

    Here is my code snippet.

    I suspect I'm using connect incorrectly.
    Thanks for advice.

    // Main Thread's source file
    MemoryInformation::MemoryInformation(QObject *parent)
        : QAbstractListModel(parent)
    {
        // omit
    }
    
    MemoryInformation::~MemoryInformation()
    {
     // Destractor is called by GUI operation (ex: Close Button)
        m_thread.quit();
        m_thread.wait();
    }
    
    void MemoryInformation::ThreadStart()
    {
        // This func is called by GUI operation
    
        Worker* pWorker = new Worker(this);
    
        // m_thread: QThread Type
        pWorker ->moveToThread(&m_thread);
    
        connect(&m_thread, &QThread::started, pWorker , &Worker::process);
        connect(pWorker , &Worker::sendElement, this, &MemoryInformation::addElement, Qt::BlockingQueuedConnection);
        connect(&m_thread, &QThread::finished, pWorker , &QObject::deleteLater, Qt::QueuedConnection);
    
        m_thread.start();
    }
    
    void MemoryInformation::addElement(Memory *element)
    {
        int pos = elements.count();
        beginInsertRows(QModelIndex(), pos, pos);
        elements.insert(pos, *element);
        endInsertRows();
    }
    
    
    // Sub Thread's source file
    Worker::Worker(MemoryInformation *pMemoryInfo, QObject *parent) : QObject(parent)
    {
        m_pMemoryInfo = pMemoryInfo;
    }
    
    Worker::~Worker()
    {
    // This function is not called
        delete m_pMemoryInfo;
    }
    
    void Worker::process()
    {
        check();
    
        auto timer = new QTimer(this);
        connect(timer, &QTimer::timeout, this, &Worker::check);
        timer->start(1000);
    }
    
    void Worker::check()
    {
        Memory *element = new <Memory>();
    
        // omit
    
        emit sendElement(element);
    }
    
    jsulmJ 1 Reply Last reply
    0
    • Y ynakane

      Hi.
      I'm a beginner of Qt multithreaded programming.

      I wrote the multithread code with reference to the Qt reference.

      But I noticed something strange.
      Workerthread's destructor does not called after QThread :: quit () on main thread.

      Here is my code snippet.

      I suspect I'm using connect incorrectly.
      Thanks for advice.

      // Main Thread's source file
      MemoryInformation::MemoryInformation(QObject *parent)
          : QAbstractListModel(parent)
      {
          // omit
      }
      
      MemoryInformation::~MemoryInformation()
      {
       // Destractor is called by GUI operation (ex: Close Button)
          m_thread.quit();
          m_thread.wait();
      }
      
      void MemoryInformation::ThreadStart()
      {
          // This func is called by GUI operation
      
          Worker* pWorker = new Worker(this);
      
          // m_thread: QThread Type
          pWorker ->moveToThread(&m_thread);
      
          connect(&m_thread, &QThread::started, pWorker , &Worker::process);
          connect(pWorker , &Worker::sendElement, this, &MemoryInformation::addElement, Qt::BlockingQueuedConnection);
          connect(&m_thread, &QThread::finished, pWorker , &QObject::deleteLater, Qt::QueuedConnection);
      
          m_thread.start();
      }
      
      void MemoryInformation::addElement(Memory *element)
      {
          int pos = elements.count();
          beginInsertRows(QModelIndex(), pos, pos);
          elements.insert(pos, *element);
          endInsertRows();
      }
      
      
      // Sub Thread's source file
      Worker::Worker(MemoryInformation *pMemoryInfo, QObject *parent) : QObject(parent)
      {
          m_pMemoryInfo = pMemoryInfo;
      }
      
      Worker::~Worker()
      {
      // This function is not called
          delete m_pMemoryInfo;
      }
      
      void Worker::process()
      {
          check();
      
          auto timer = new QTimer(this);
          connect(timer, &QTimer::timeout, this, &Worker::check);
          timer->start(1000);
      }
      
      void Worker::check()
      {
          Memory *element = new <Memory>();
      
          // omit
      
          emit sendElement(element);
      }
      
      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @ynakane Why should the destructor be called if you terminate a thread? The QThread object still exists, destructor will be called as soon as the QThread instance is destroyed.

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

      Y 1 Reply Last reply
      5
      • jsulmJ jsulm

        @ynakane Why should the destructor be called if you terminate a thread? The QThread object still exists, destructor will be called as soon as the QThread instance is destroyed.

        Y Offline
        Y Offline
        ynakane
        wrote on last edited by
        #3

        @jsulm Thanks for your comment.
        This problem has been resolved
        Thank you.

        1 Reply Last reply
        1

        • Login

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