Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6710265
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:00:06+00:00 2026-05-26T08:00:06+00:00

I have two classes – one that runs in the main thread and performs

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T08:00:07+00:00Added an answer on May 26, 2026 at 8:00 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two classes. One(Person) for getters and setters, and another(People) for compute the
I have two classes that are almost identical, besides one method. The classes have
I have two classes, and want to include a static instance of one class
I have two classes, Foo and Bar, that have constructors like this: class Foo
I have two classes that each need an instance of each other to function.
I have two classes defined such that they both contain references to the other
I have two classes, one nested in the other. Public Class Operation Public Property
I have two classes that my pages and controls inherit from. These two classes
I have two classes: Action class, that has a method for executing VBScript files,
I have two classes that are already subclasses of a different parent (due to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.