为什么这个类的析构函数没有被调用?

发布于 2024-12-11 13:27:44 字数 704 浏览 0 评论 0原文

我有两个类 - 一个在主线程中运行并执行 GUI 操作,另一个执行一些计算并发出网络请求。

// A member of the class that runs in the main thread
QThread 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();

在主线程中运行的类的析构函数中:

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

我期望发生的是线程终止并销毁 CServerThread 的实例类。但是,CServerThread 类的析构函数没有被调用。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

羁客 2024-12-18 13:27:44

QThread::quit() 停止该线程的事件循环。

告诉线程的事件循环退出并返回代码0(成功)。

但是 QObject::deleteLater() 需要“拥有”线程处于活动状态的事件循环:

安排删除该对象。
当控制权返回到事件循环时,该对象将被删除。

因此,您的对象的析构函数将不会运行,finished 信号的触发为时已晚。

考虑下面的人为示例:

#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;
        }
};

如果调用:

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

输出将为:

Started
Has quit
Done waiting
Finished

finishedwait 完成后触发。您的 deleteLater 连接要生效已经太晚了,该线程的事件循环已经死了。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文