您的位置:首页 > 编程语言 > Qt开发

Qt5使用内存泄露检测工具—VLD

2015-04-21 23:39 676 查看
本文简要描述一下在Qt应用中使用VLD来检测内存泄露。本次测试环境所用的Qt版本是:qt-opensource-windows-x86-msvc2013_64-5.4.0

一、VLD工具概述
Visual Leak Detector(VLD)是一款用于Visual C++的免费的内存泄露检测工具。他的特点有:可以得到内存泄漏点的调用堆栈,如果可以的话,还可以得到其所在文件及行号;
可以得到泄露内存的完整数据; 可以设置内存泄露报告的级别;并且是开源免费的。

二、VLD的下载和安装
下载地址:http://vld.codeplex.com/
我所用的版本是2.4,安装时注意把下图的选项全勾上,这才会把vld的dll文件拷贝到PATH环境变量中,否则会找不到vld的dll.

将“vld安装目录/include”下的vld.h文件拷贝到Qt默认的include目录下,将“vld安装目录/lib/Win64"下的vld.lib拷贝到Qt默认的lib目录,就完成安装了。

四、VLDd在Qt的使用
创建测试项目:使用Qt Creator创建一个Qt Widgets Application,要在Debug模式下运行
修改main.cpp文件,在main函数上面添加以下代码:
include<vld.h>

测试1:在MainWindow的构造函数中添加一行代码
new QWidget(this); // 不会泄露
编译运行,在QtCreator的应用程序输出窗口中将会有类似下面的内容:
Visual Leak Detector Version 2.4RC2 installed.
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工具进行检测。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: