经过对我的 QT 应用程序进行大量测试和更改后,Visual Leak Detector 识别出了令人讨厌的泄漏源(8 字节)。 VLD 报告 QT 应用程序是干净的,除了QThread*
指针。
一些实现背景:该应用程序由 Jeffrey Holmes 建模为解决方案的混合体,使用Qt批量下载网页 https://stackoverflow.com/questions/3910105/bulk-download-of-web-pages-using-qt。感谢 Jeffrey 提供的早期解决方案!
问题:
Code:
void vqMDIChildDialog::processWorkQueue(bool bIsBC)
{
if (m_listOfTables.isEmpty() && currentReplicationThreads == 0)
{
}
else if (!m_listOfTables.isEmpty())
{
for (int i = 0; i < maxReplicationThreads && !m_listOfTables.isEmpty();i++)
{
QThread *thread = new QThread;
QPointer<vcSharedDataQt> worker = new vcSharedDataQt();
worker->moveToThread(thread);
QString tmpTableName (m_listOfTables.dequeue());
worker->setParentObject(this);
//
// set properties on the worker object.
//
connect(thread, SIGNAL(started()), worker, SLOT(process()));
connect(worker, SIGNAL(finished()), thread, SLOT(quit()));
connect(worker, SIGNAL(finished()), worker, SLOT(deleteLater()));
connect(thread, SIGNAL(finished()), thread,SLOT(deleteLater()));
connect(worker,
SIGNAL(updateMessageFromThread( const QString&,
const QString&,
const QString&,
const QString&,
const QString&,
const QString&,
const QString&)),
this,
SLOT(UpdateStatusBarFromThread( const QString&,
const QString&,
const QString&,
const QString&,
const QString&,
const QString&,
const QString&)));
thread->setObjectName(worker->getUniqueKey());
thread->start();
currentReplicationThreads ++;
}
}
}
Stack 不允许我回答这个问题,所以:
该功能受到保护QMutex
:
mutex.lock();
processWorkQueue();
mutex.unlock();
这导致了内存泄漏。QThread
显然无法在工作线程完成时销毁。我删除了互斥锁,VLD 报告没有内存泄漏QThread
.