Visual Leak Detector 报告 qt 库泄漏

Posted

技术标签:

【中文标题】Visual Leak Detector 报告 qt 库泄漏【英文标题】:Visual Leak Detector reports qt library leaks 【发布时间】:2018-03-06 16:06:21 【问题描述】:

我创建了一个 Qt 4.7.4 程序,然后包含 vld 库来检测内存泄漏。结果是我得到了太多泄漏,大多数是 Qt 库泄漏,这很奇怪。有没有人遇到过这个问题?

memory_leak_report.txt是这样的:

Visual Leak Detector Version 2.5.1 installed.
    Outputting the report to C:\Users\kelgharb\Documents\Visual Studio 2010\Projects\Entreprise\EntrepriseCreator\memory_leak_report.txt WARNING: Visual Leak Detector detected memory leaks!
---------- Block 25 at 0x00E07E58: 20 bytes ----------   Leak Hash: 0x5688712A, Count: 1, Total 20 bytes   Call Stack (TID 1348):
    MSVCR100D.dll!operator new()
    c:\users\kelgharb\documents\visual studio 2010\projects\entreprise\entreprisecreator\generatedfiles\ui_createentreprise.h (50): EntrepriseCreator.exe!Ui_CreateEntrepriseClass::setupUi() + 0x7 bytes
    c:\users\kelgharb\documents\visual studio 2010\projects\entreprise\entreprisecreator\createentreprise.cpp (10): EntrepriseCreator.exe!CreateEntreprise::CreateEntreprise()
    c:\users\kelgharb\documents\visual studio 2010\projects\entreprise\entreprisecreator\entreprisecreator.cpp (45): EntrepriseCreator.exe!EntrepriseCreator::onTrigger_createEntreprise()
+ 0x31 bytes
    c:\users\kelgharb\documents\visual studio 2010\projects\entreprise\entreprisecreator\generatedfiles\debug\moc_entreprisecreator.cpp (82): EntrepriseCreator.exe!EntrepriseCreator::qt_metacall() + 0x8 bytes
    c:\qt\4.7.4\src\corelib\kernel\qmetaobject.cpp (238): QtCored4.dll!QMetaObject::metacall()
    c:\qt\4.7.4\src\corelib\kernel\qobject.cpp (3278): QtCored4.dll!QMetaObject::activate() + 0x27 bytes
    c:\qt\4.7.4\src\gui\tmp\moc\debug_shared\moc_qaction.cpp (263): QtGuid4.dll!QAction::triggered() + 0x15 bytes
    c:\qt\4.7.4\src\gui\kernel\qaction.cpp (1259): QtGuid4.dll!QAction::activate()
    c:\qt\4.7.4\src\gui\widgets\qmenu.cpp (1007): QtGuid4.dll!QMenuPrivate::activateCausedStack()
    c:\qt\4.7.4\src\gui\widgets\qmenu.cpp (1100): QtGuid4.dll!QMenuPrivate::activateAction()
    c:\qt\4.7.4\src\gui\widgets\qmenu.cpp (2297): QtGuid4.dll!QMenu::mouseReleaseEvent()
    c:\qt\4.7.4\src\gui\kernel\qwidget.cpp (8296): QtGuid4.dll!QWidget::event()
    c:\qt\4.7.4\src\gui\widgets\qmenu.cpp (2406): QtGuid4.dll!QMenu::event()
    c:\qt\4.7.4\src\gui\kernel\qapplication.cpp (4481): QtGuid4.dll!QApplicationPrivate::notify_helper() + 0x11 bytes
    c:\qt\4.7.4\src\gui\kernel\qapplication.cpp (4042): QtGuid4.dll!QApplication::notify() + 0x2F bytes
    c:\qt\4.7.4\src\corelib\kernel\qcoreapplication.cpp (787): QtCored4.dll!QCoreApplication::notifyInternal() + 0x15 bytes
    c:\qt\4.7.4\src\corelib\kernel\qcoreapplication.h (218): QtCored4.dll!QCoreApplication::sendSpontaneousEvent() + 0x38 bytes
    c:\qt\4.7.4\src\gui\kernel\qapplication.cpp (3139): QtGuid4.dll!QApplicationPrivate::sendMouseEvent() + 0xE bytes
    c:\qt\4.7.4\src\gui\kernel\qapplication_win.cpp (3234): QtGuid4.dll!QETWidget::translateMouseEvent() + 0x2A bytes
    c:\qt\4.7.4\src\gui\kernel\qapplication_win.cpp (1659): QtGuid4.dll!QtWndProc() + 0xC bytes
    USER32.dll!gapfnScSendMessage() + 0x332 bytes
    USER32.dll!GetThreadDesktop() + 0xD7 bytes
    USER32.dll!CharPrevW() + 0x138 bytes
    USER32.dll!DispatchMessageW() + 0xF bytes
    c:\qt\4.7.4\src\corelib\kernel\qeventdispatcher_win.cpp (810): QtCored4.dll!QEventDispatcherWin32::processEvents()
    c:\qt\4.7.4\src\gui\kernel\qapplication_win.cpp (1170): QtGuid4.dll!QGuiEventDispatcherWin32::processEvents() + 0x15 bytes
    c:\qt\4.7.4\src\corelib\kernel\qeventloop.cpp (150): QtCored4.dll!QEventLoop::processEvents()
    c:\qt\4.7.4\src\corelib\kernel\qeventloop.cpp (201): QtCored4.dll!QEventLoop::exec() + 0x2D bytes
    c:\qt\4.7.4\src\corelib\kernel\qcoreapplication.cpp (1064): QtCored4.dll!QCoreApplication::exec() + 0x15 bytes
    c:\qt\4.7.4\src\gui\kernel\qapplication.cpp (3756): QtGuid4.dll!QApplication::exec()
    c:\users\kelgharb\documents\visual studio 2010\projects\entreprise\entreprisecreator\main.cpp (12): EntrepriseCreator.exe!main() + 0x6 bytes
    c:\qt\4.7.4\src\winmain\qtmain_win.cpp (131): EntrepriseCreator.exe!WinMain() + 0x12 bytes
    f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (547): EntrepriseCreator.exe!__tmainCRTStartup() + 0x2C bytes
    f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (371): EntrepriseCreator.exe!WinMainCRTStartup()
    kernel32.dll!BaseThreadInitThunk() + 0x12 bytes
    ntdll.dll!RtlInitializeExceptionChain() + 0x63 bytes
    ntdll.dll!RtlInitializeExceptionChain() + 0x36 bytes   Data:
    D4 50 01 01    18 0A 92 02    AC 50 01 01    00 00 CD CD     .P...... .P......
    D0 0A 92 02                                                  ........ ........
---------- Block 22 at 0x02892950: 52 bytes ----------   Leak Hash: 0x04BB07FE, Count: 1, Total 52 bytes   Call Stack (TID 1348):
    MSVCR100D.dll!operator new()
    c:\users\kelgharb\documents\visual studio 2010\projects\entreprise\entreprisecreator\entreprisecreator.cpp (45): EntrepriseCreator.exe!EntrepriseCreator::onTrigger_createEntreprise()
+ 0x7 bytes
    c:\users\kelgharb\documents\visual studio 2010\projects\entreprise\entreprisecreator\generatedfiles\debug\moc_entreprisecreator.cpp (82): EntrepriseCreator.exe!EntrepriseCreator::qt_metacall() + 0x8 bytes
    c:\qt\4.7.4\src\corelib\kernel\qmetaobject.cpp (238): QtCored4.dll!QMetaObject::metacall()
    c:\qt\4.7.4\src\corelib\kernel\qobject.cpp (3278): QtCored4.dll!QMetaObject::activate() + 0x27 bytes
    c:\qt\4.7.4\src\gui\tmp\moc\debug_shared\moc_qaction.cpp (263): QtGuid4.dll!QAction::triggered() + 0x15 bytes
    c:\qt\4.7.4\src\gui\kernel\qaction.cpp (1259): QtGuid4.dll!QAction::activate()
    c:\qt\4.7.4\src\gui\widgets\qmenu.cpp (1007): QtGuid4.dll!QMenuPrivate::activateCausedStack()
    c:\qt\4.7.4\src\gui\widgets\qmenu.cpp (1100): QtGuid4.dll!QMenuPrivate::activateAction()
    c:\qt\4.7.4\src\gui\widgets\qmenu.cpp (2297): QtGuid4.dll!QMenu::mouseReleaseEvent()
    c:\qt\4.7.4\src\gui\kernel\qwidget.cpp (8296): QtGuid4.dll!QWidget::event()
    c:\qt\4.7.4\src\gui\widgets\qmenu.cpp (2406): QtGuid4.dll!QMenu::event()
    c:\qt\4.7.4\src\gui\kernel\qapplication.cpp (4481): QtGuid4.dll!QApplicationPrivate::notify_helper() + 0x11 bytes
    c:\qt\4.7.4\src\gui\kernel\qapplication.cpp (4042): QtGuid4.dll!QApplication::notify() + 0x2F bytes
    c:\qt\4.7.4\src\corelib\kernel\qcoreapplication.cpp (787): QtCored4.dll!QCoreApplication::notifyInternal() + 0x15 bytes
    c:\qt\4.7.4\src\corelib\kernel\qcoreapplication.h (218): QtCored4.dll!QCoreApplication::sendSpontaneousEvent() + 0x38 bytes
    c:\qt\4.7.4\src\gui\kernel\qapplication.cpp (3139): QtGuid4.dll!QApplicationPrivate::sendMouseEvent() + 0xE bytes
    c:\qt\4.7.4\src\gui\kernel\qapplication_win.cpp (3234): QtGuid4.dll!QETWidget::translateMouseEvent() + 0x2A bytes
    c:\qt\4.7.4\src\gui\kernel\qapplication_win.cpp (1659): QtGuid4.dll!QtWndProc() + 0xC bytes
    USER32.dll!gapfnScSendMessage() + 0x332 bytes

【问题讨论】:

Qt 中存在各种内存泄漏。如果没有浏览代码并修复它们,那么您对它们无能为力。考虑为 Qt 添加过滤器或异常,这样它就不会在您自己的代码中隐藏内存泄漏。 Khaled 为什么还在 Qt4.7 上,只是测试?它太旧了,现在您可以使用 Qt 5.10.1,并在 Qt Creator 4.5.1 中参考 Detecting Memory Leaks @MohammadKanan 没错,我只是在测试一个程序。 【参考方案1】:

与 Qt 相关的内存泄漏是由于 QDialogs 造成的,我每次创建新的 QDialog 对象时都缺少一行 setAttribute(Qt::WA_DeleteOnClose)

void EntrepriseCreator::onTrigger_createEntreprise()

    CreateEntreprise* entreprise = new CreateEntreprise();
    entreprise->setAttribute(Qt::WA_DeleteOnClose);
    QObject::connect(entreprise, SIGNAL(disable_actionEntreprise(bool)), this, SLOT(disable_actionEntreprise(bool)));
    entreprise->exec();

【讨论】:

以上是关于Visual Leak Detector 报告 qt 库泄漏的主要内容,如果未能解决你的问题,请参考以下文章

Visual Leak Detector配置项 ReportEncoding

Visual Leak Detector在 QT 中使用 VLD(方式一)

Visual Leak Detector在 QT 中使用 VLD(方式二)

Visual Leak Detector配置项 ReportTo

Visual Leak Detector使用注意事项

Visual Leak Detector简介