Search code examples
qtdestructorqthread

Why isn't the destructor of this class being invoked?


I have two classes - one that runs in the main thread and performs GUI operations and another that performs some calculations and makes network requests.

// A member of the class that runs in the main thread
QThread thread;

Here is a snippet from the initialization method of the class that runs in the main thread:

// Create the class that runs in the other thread and move it there
CServerThread * server = new CServerThread;
server->moveToThread(&thread);

// When the thread terminates, we want the object destroyed
connect(&thread, SIGNAL(finished()), server, SLOT(deleteLater()));
thread.start();

In the destructor for the class that runs in the main thread:

if(thread.isRunning())
{
    thread.quit();
    thread.wait();
}

What I expect to happen is the thread terminates and destroys the instance of the CServerThread class. However, the destructor for the CServerThread class is not getting invoked.


Solution

  • QThread::quit() stops the event loop for that thread.

    Tells the thread's event loop to exit with return code 0 (success).

    But QObject::deleteLater() needs the event loop for the "owning" thread to be active:

    Schedules this object for deletion.
    The object will be deleted when control returns to the event loop.

    So your object's destructor will not run, the finished signal is fired too late for that.

    Consider the contrived example below:

    #include <QThread>
    #include <iostream>
    
    class T: public QObject
    {
        Q_OBJECT
    
        public:
            QThread thr;
            T() {
                connect(&thr, SIGNAL(finished()), this, SLOT(finished()));
            };
            void start() {
                thr.start();
                std::cout << "Started" << std::endl;
            }
            void stop() {
                thr.quit();
                std::cout << "Has quit" << std::endl;
            }
            void end() {
                thr.wait();
                std::cout << "Done waiting" << std::endl;
            }
        public slots:
            void finished() {
                std::cout << "Finished" << std::endl;
            }
    };
    

    If you call:

    T t;
    t.start();
    t.stop();
    t.end();
    

    The output will be:

    Started
    Has quit
    Done waiting
    Finished
    

    finished is triggered after the wait is done. Too late for your deleteLater connection to be effective, that thread's event loop is already dead.