我有一个类:
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();
}
我遇到了内存泄漏。
在销毁拥有者对象期间终止线程的正确方法是什么?
QThread
的情况下调用它。在这种情况下,您必须以某种方式向正在执行线程的方法发出信号,告诉它必须结束。无论如何,这是最正确的方法,而不会泄漏。 - Vinícius Gobbo A. de Oliveira