QThread::quit() does not call workerthread's destractor
Solved
General and Desktop
-
Hi.
I'm a beginner of Qt multithreaded programming.I wrote the multithread code with reference to the Qt reference.
But I noticed something strange.
Workerthread's destructor does not called after QThread :: quit () on main thread.Here is my code snippet.
I suspect I'm using connect incorrectly.
Thanks for advice.// Main Thread's source file MemoryInformation::MemoryInformation(QObject *parent) : QAbstractListModel(parent) { // omit } MemoryInformation::~MemoryInformation() {  // Destractor is called by GUI operation (ex: Close Button) m_thread.quit(); m_thread.wait(); } void MemoryInformation::ThreadStart() { // This func is called by GUI operation Worker* pWorker = new Worker(this); // m_thread: QThread Type pWorker ->moveToThread(&m_thread); connect(&m_thread, &QThread::started, pWorker , &Worker::process); connect(pWorker , &Worker::sendElement, this, &MemoryInformation::addElement, Qt::BlockingQueuedConnection); connect(&m_thread, &QThread::finished, pWorker , &QObject::deleteLater, Qt::QueuedConnection); m_thread.start(); } void MemoryInformation::addElement(Memory *element) { int pos = elements.count(); beginInsertRows(QModelIndex(), pos, pos); elements.insert(pos, *element); endInsertRows(); }
// Sub Thread's source file Worker::Worker(MemoryInformation *pMemoryInfo, QObject *parent) : QObject(parent) { m_pMemoryInfo = pMemoryInfo; } Worker::~Worker() { // This function is not called delete m_pMemoryInfo; } void Worker::process() { check(); auto timer = new QTimer(this); connect(timer, &QTimer::timeout, this, &Worker::check); timer->start(1000); } void Worker::check() { Memory *element = new <Memory>(); // omit emit sendElement(element); }