This article briefly describes the use of VLD in QT applications to detect memory leaks. This test environment: QtCreator2.3 + qt4.7.4-vs2008 + VS2008 Express.
1. Download and install: VLD-2.2: http://vld.codeplex.com/
Links:http://vld.codeplex.com/releases/70398/download/261406
Assume the installation to the c:/dev/vld-2.2 directory.
Note: VLD Originally published in CodeProject.com, this version is too old. No detection, no use.
2. Create a test project: Use Qtcreator to create a QT GUI project.
Modify the. Pro file to add the following:
- Win32 {
- CONFIG (Debug, Debug|release) {
- # defines + = _DEBUG
- # VLD 2.2 Downloaded from http://vld.codeplex.com/
- Vld_path = c:/dev/vld-2.2
- Includepath + = $VLD _path/include
- LIBS + =-L$VLD_PATH/LIB/WIN32-LVLD
- }
- }
To modify the Main.cpp file, add the following code above the main function:
- #ifdef _DEBUG
- #include "vld.h"
- #endif
3, to test:
Test 1: Add a line of code to the MainWindow constructor:
- New Qwidget (this); Will not leak
Compile run, there will be something like the following in the Qtcreator Application Output window:
- Visual Leak Detector Version 2.2 installed.
- Visual Leak Detector Version 2.2 installed.
- No memory leaks detected.
- Visual Leak Detector is now exiting.
The above indicates that no memory leaks were found.
(The initial run may not work because the VLD DLL file cannot be found.) Copy the contents of the C:\dev\vld-2.2\bin\Win32 directory to a directory listed in the PATH environment variable)
Test 2: Add another line of code:
- New Qwidget (0); //This will leak
Compile again to run with the result:
- Visual Leak Detector Version 2.2 installed.
- Warning:visual Leak Detector detected memory leaks!
- ----------Block 8 at 0x00ef14e0:20 bytes----------
- Call Stack:
- E:\works\test_vld_qt\mainwindow.cpp (): test_vld_qt.exe! Mainwindow::mainwindow + 0x7 bytes
- E:\works\test_vld_qt\main.cpp: Test_vld_qt.exe!main + 0xA bytes
- E:\qt\4.7.4-vs2008\src\winmain\qtmain_win.cpp (131): test_vld_qt.exe! WinMain + 0x12 bytes
- F:\DD\VCTOOLS\CRT_BLD\SELF_X86\CRT\SRC\CRTEXE.C (578): Test_vld_qt.exe!__tmaincrtstartup + 0x35 bytes
- F:\DD\VCTOOLS\CRT_BLD\SELF_X86\CRT\SRC\CRTEXE.C (403): test_vld_qt.exe! WinMainCRTStartup
- 0x7c817067 (File and line number not available): kernel32.dll! Registerwaitforinputidle + 0x49 bytes
- Data:
- 8C 8A F8 Bayi F1 (8A)/CD [email protected] [email protected]
- B0 F1 00 ........ .....
- Visual Leak Detector detected 1 memory Leak (bytes).
- Largest number used:432 bytes.
- Total allocations:432 bytes.
- Visual Leak Detector is now exiting.
A memory leak was detected this time.
Summary: As shown above, using VLD to detect memory leaks is easy, in the ointment is only using VC + + compiler. However, we can also use it to detect memory leaks under WIN32, and then use other compilers to compile and publish on other platforms.
Detecting memory leaks in QT applications--VLD