Detecting memory leaks in QT applications--VLD

Source: Internet
Author: User

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:

  1. Win32 {
  2. CONFIG (Debug, Debug|release) {
  3. # defines + = _DEBUG
  4. # VLD 2.2 Downloaded from http://vld.codeplex.com/
  5. Vld_path = c:/dev/vld-2.2
  6. Includepath + = $VLD _path/include
  7. LIBS + =-L$VLD_PATH/LIB/WIN32-LVLD
  8. }
  9. }

To modify the Main.cpp file, add the following code above the main function:

    1. #ifdef _DEBUG
    2. #include "vld.h"
    3. #endif

3, to test:

Test 1: Add a line of code to the MainWindow constructor:

    1. New Qwidget (this); Will not leak
Compile run, there will be something like the following in the Qtcreator Application Output window:
    1. Visual Leak Detector Version 2.2 installed.
    2. Visual Leak Detector Version 2.2 installed.
    3. No memory leaks detected.
    4. 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:

    1. New Qwidget (0); //This will leak
Compile again to run with the result:
  1. Visual Leak Detector Version 2.2 installed.
  2. Warning:visual Leak Detector detected memory leaks!
  3. ----------Block 8 at 0x00ef14e0:20 bytes----------
  4. Call Stack:
  5. E:\works\test_vld_qt\mainwindow.cpp (): test_vld_qt.exe! Mainwindow::mainwindow + 0x7 bytes
  6. E:\works\test_vld_qt\main.cpp: Test_vld_qt.exe!main + 0xA bytes
  7. E:\qt\4.7.4-vs2008\src\winmain\qtmain_win.cpp (131): test_vld_qt.exe! WinMain + 0x12 bytes
  8. F:\DD\VCTOOLS\CRT_BLD\SELF_X86\CRT\SRC\CRTEXE.C (578): Test_vld_qt.exe!__tmaincrtstartup + 0x35 bytes
  9. F:\DD\VCTOOLS\CRT_BLD\SELF_X86\CRT\SRC\CRTEXE.C (403): test_vld_qt.exe! WinMainCRTStartup
  10. 0x7c817067 (File and line number not available): kernel32.dll! Registerwaitforinputidle + 0x49 bytes
  11. Data:
  12. 8C 8A F8 Bayi F1 (8A)/CD [email protected] [email protected]
  13. B0 F1 00 ........ .....
  14. Visual Leak Detector detected 1 memory Leak (bytes).
  15. Largest number used:432 bytes.
  16. Total allocations:432 bytes.
  17. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.