No memory leaks detected.
Visual Leak Detector is now exiting.
Visual Leak Detector Version 2.4RC2 installed.
No memory leaks detected.
Visual Leak Detector is now exiting.
以上表示没有发现内存泄露。
测试2:在MainWindow的构造函数中添加一行代码:
new QWidget(0) ; // 这个会泄露
再次编译运行,结果为:
Visual Leak Detector Version 2.4RC2 installed.
WARNING: Visual Leak Detector detected memory leaks!
---------- Block 9 at 0x00000000025FB4C0: 48 bytes ----------
Leak Hash: 0x8068CA22, Count: 1, Total 48 bytes
Call Stack (TID 10720):
0x00000000E35CAFC0 (File and line number not available): MSVCR120D.dll!operator new
e:\mydevelop\qt project\test\vldtest\mainwindow.cpp (11): VLDTest.exe!MainWindow::MainWindow + 0xA bytes
e:\mydevelop\qt project\test\vldtest\main.cpp (12): VLDTest.exe!main + 0xC bytes
c:\work\build\qt5_workdir\w\s\qtbase\src\winmain\qtmain_win.cpp (112): VLDTest.exe!WinMain + 0xE bytes
f:\dd\vctools\crt\crtw32\dllstuff\crtexe.c (618): VLDTest.exe!__tmainCRTStartup + 0x1B bytes
f:\dd\vctools\crt\crtw32\dllstuff\crtexe.c (466): VLDTest.exe!WinMainCRTStartup
0x00000000771C59CD (File and line number not available): kernel32.dll!BaseThreadInitThunk + 0xD bytes
0x00000000772FB891 (File and line number not available): ntdll.dll!RtlUserThreadStart + 0x21 bytes
Data:
78 DD 16 3F 01 00 00 00 50 FB 2C 00 00 00 00 00 x..?.... P.,.....
38 DF 16 3F 01 00 00 00 00 00 CD CD CD CD CD CD 8..?.... ........
00 00 00 00 00 00 00 00 80 FC 2C 00 00 00 00 00 ........ ..,.....
Visual Leak Detector detected 1 memory leak (100 bytes).
Largest number used: 902 bytes.
Total allocations: 902 bytes.
Visual Leak Detector is now exiting.
Visual Leak Detector Version 2.4RC2 installed.
WARNING: Visual Leak Detector detected memory leaks!
---------- Block 9 at 0x00000000025FB4C0: 48 bytes ----------
Leak Hash: 0x8068CA22, Count: 1, Total 48 bytes
Call Stack (TID 10720):
0x00000000E35CAFC0 (File and line number not available): MSVCR120D.dll!operator new
e:\mydevelop\qt project\test\vldtest\mainwindow.cpp (11): VLDTest.exe!MainWindow::MainWindow + 0xA bytes
e:\mydevelop\qt project\test\vldtest\main.cpp (12): VLDTest.exe!main + 0xC bytes
c:\work\build\qt5_workdir\w\s\qtbase\src\winmain\qtmain_win.cpp (112): VLDTest.exe!WinMain + 0xE bytes
f:\dd\vctools\crt\crtw32\dllstuff\crtexe.c (618): VLDTest.exe!__tmainCRTStartup + 0x1B bytes
f:\dd\vctools\crt\crtw32\dllstuff\crtexe.c (466): VLDTest.exe!WinMainCRTStartup
0x00000000771C59CD (File and line number not available): kernel32.dll!BaseThreadInitThunk + 0xD bytes
0x00000000772FB891 (File and line number not available): ntdll.dll!RtlUserThreadStart + 0x21 bytes
Data:
78 DD 16 3F 01 00 00 00 50 FB 2C 00 00 00 00 00 x..?.... P.,.....
38 DF 16 3F 01 00 00 00 00 00 CD CD CD CD CD CD 8..?.... ........
00 00 00 00 00 00 00 00 80 FC 2C 00 00 00 00 00 ........ ..,.....
Visual Leak Detector detected 1 memory leak (100 bytes).
Largest number used: 902 bytes.
Total allocations: 902 bytes.
Visual Leak Detector is now exiting.
这次检测到了内存泄露。
如上所示,使用vld检测内存泄露很容易,美中不足的是只能使用VC++编译器。尽管如此,我们也可以用它来在Win32下检测内存泄露,然后再使用其它编译器在其它平台上进行编译发布。
关于linux的内存泄露,可以使用valgrind工具进行检测。