【发布时间】:2015-04-24 00:19:13
【问题描述】:
我有一堂课:
class centralDataPool : public QObject
{
Q_OBJECT
public:
centralDataPool(QObject * parent = 0);
~centralDataPool();
commMonitor commOverWatch;
private:
QThread monitorThread;
int totalNum;
signals:
void createMonitor(int);
};
在它的构造函数中我做了:
centralDataPool::centralDataPool(QObject* parent) : QObject(parent),totalNum(0)
{
connect(this, SIGNAL(createMonitor(int)), &commOverWatch, SLOT(createMonitor(int)));
commOverWatch.moveToThread(&monitorThread);
monitorThread.start();
}
当我调用此类的析构函数时,我收到错误消息:
qthread destroyed while thread is still running
但是当我试图在类centralDataPool的析构函数中终止monitorThread时,
centralDataPool::~centralDataPool()
{
monitorThread.terminate();
}
我得到内存泄漏。
在销毁其所有者对象期间终止线程的正确方法是什么?
【问题讨论】:
-
你的析构代码在哪里?
-
@ViníciusGobboA.deOliveira 查看编辑。
-
你不应该完成这样的线程。查看文档:doc.qt.io/qt-5/qthread.html#terminate
-
@ViníciusGobboA.deOliveira,您好,我尝试添加 wait() 或切换到 quit(),但都不起作用...您能提供更多提示吗?
-
哦!我错过了。它是一个静态受保护的方法,因此如果不从
QThread继承就无法调用它。在这种情况下,您必须以某种方式向线程上正在执行的方法发出信号,表明它必须结束。无论如何,这是不泄漏的最正确方法。
标签: c++ multithreading qt qthread