Qt有没有mfc内存泄露检测工具具

测试程序:
vld-2.2.3:&
参考链接1:&
参考链接2:&
参考链接1和参考链接2说的是一个东西,不使用任何工具,自己写程序判断溢出位置。
参考链接3:&
参考链接4:&
参考链接3和参考链接4是用VLD(Visual Leak Detector)来检测内存溢出,当然更加专业。
问题描述:
今天在QT_VP中简单地添加了内存泄露检测语句:_CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );结果出来一大堆的内存泄露,着实吓了一跳,跟踪了一天,逐段派出,最后还是感觉没问题(还是比较自信代码质量的O(&_&)O哈哈~)。。。最后上网一查,在vs中,先是进行内存泄露报告,然后才卸载Qt的dll,释放申请的资源。所以才会出现这种情况。
系统环境:
windows7 + VS2008;
操作方法与参考链接1做法一致,如下:
标志内存检测报告:setdebugnew.h(注意,只有在cpp中添加setdebugnew.h,而且有new操作的那些函数或者类被调用,才会进行内存泄露跟踪)
#ifndef&SET_DEBUG_NEW_H
#define&SET_DEBUG_NEW_H
#ifdef&_DEBUG
#define&DEBUG_CLIENTBLOCK&&&new(&_CLIENT_BLOCK,&__FILE__,&__LINE__)
#define DEBUG_CLIENTBLOCK
#define&_CRTDBG_MAP_ALLOC
#include&&crtdbg.h&
#ifdef&_DEBUG
#define&new&DEBUG_CLIENTBLOCK
#endif&// end SET_DEBUG_NEW_H
在需要检测的cpp文件中包含setdebugnew.h头文件
test_memoryleak.cpp
#include&"test_memoryleak.h"
#include&&QtGui&
#include&"setdebugnew.h"
test_memoryLeak::test_memoryLeak(QWidget&*parent,&Qt::WFlags&flags)
&&&&&&:&QWidget(parent,&flags)
&&&&&&btn1&=&new&QPushButton("test1",&this);
&&&&&&btn2&=&new&QPushButton("close",&this);
&&&&&&QWidget&*memLeak_test&=&new&QW
&&&&&&connect(btn2,&SIGNAL(clicked()),&this,&SLOT(close()));
&&&&&&QHBoxLayout&*layout&=&new&QHBoxL
&&&&&&layout-&addWidget(btn1);
&&&&&&layout-&addWidget(btn2);
&&&&&&setLayout(layout);
test_memoryLeak::~test_memoryLeak()
main.cpp(在主函数入口处设置_CrtSetDbgFlag&(&_CRTDBG_ALLOC_MEM_DF&|&_CRTDBG_LEAK_CHECK_DF&);)
#include&"test_memoryleak.h"
#include&&QtGui/QApplication&
#include&"setdebugnew.h"
int&main(int&argc,&char&*argv[])
#ifdef&_DEBUG&
&&&&&&_CrtSetDbgFlag&(&_CRTDBG_ALLOC_MEM_DF&|&_CRTDBG_LEAK_CHECK_DF&);
&&&&&&int&*i&=&new∫
&&&&&&QApplication&a(argc,&argv);
&&&&&&test_memoryLeak&w;
&&&&&&w.show();
&&&&&&return&a.exec();
启动调试,在程序运行结束后,查看&输出&项中的内容
{1220} normal block at 0x, 552 bytes long.
&Data: &&&&&&&&&& f&&&& & 83 00 00 00 83 00 00 00 08 06 66 01 CD CD CD CD
{1215} normal block at 0x2 bytes long.
&Data: &&& eH f&&&&& x=g& 94 85 A2 65 48 03 66 01 00 00 00 00 C4 78 3D 67
.\test_memoryleak.cpp(12) : {1214} client block at 0x, subtype 0, 20 bytes long.
{289} normal block at 0x bytes long.
&Data: &&&&&& 8&&&&&&&& & 03 00 00 00 88 8A 38 01 00 CD CD CD 00 00 00 00
.\main.cpp(12) : {285} client block at 0x, subtype 0, 4 bytes long.
&Data: &&&& & CD CD CD CD
Object dump complete.
黑体加粗部分的才是程序真正溢出的地方。
使用VLD检测内存溢出,VLD下载地址:&(2.2.3版只支持VS )
安装过程会弹出如下对话框,
新版解决了path问题,所以我们无需做过多的设置。
安装完成后,还需要对VS的编译路径进行设置,
在&工具&-&&选项&-&&项目和解决方案&-&"VC++目录" 添加VLD的&包含文件&路径和&库文件&路径,如下如所示:
注意:参考链接4提到了,在win7中需要拷贝dbghelp.dll和vdl_x86.dll到system32目录,而我在配置的时候并不需要这么做。
在main.cpp中包含vld.h头文件即可,如下:
#include&"test_memoryleak.h"
#include&&QtGui/QApplication&
#ifdef&_DEBUG&
#include&"vld.h"&
#include&"setdebugnew.h"
int&main(int&argc,&char&*argv[])
#ifdef&_DEBUG&
&&&&&&_CrtSetDbgFlag&(&_CRTDBG_ALLOC_MEM_DF&|&_CRTDBG_LEAK_CHECK_DF&);
&&&&&&int&*i&=&new∫
&&&&&&QApplication&a(argc,&argv);
&&&&&&test_memoryLeak&w;
&&&&&&w.show();
&&&&&&return&a.exec();
启动调试,在程序运行结束后,查看&输出&项中的内容
WARNING: Visual Leak Detector detected memory leaks!
---------- Block 1 at 0x007F72B0: 4 bytes ----------
& Call Stack:
&&& c:\users\ajaxhe\desktop\program\qt\practice\test_memoryleak\test_memoryleak\main.cpp (17): test_memoryLeak.exe!main + 0x10 bytes
&&& C:\Qt\4.7.4\src\winmain\qtmain_win.cpp (131): test_memoryLeak.exe!WinMain + 0x12 bytes
&&& f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (578): test_memoryLeak.exe!__tmainCRTStartup + 0x35 bytes
&&& f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (403): test_memoryLeak.exe!WinMainCRTStartup
&&& 0x76B9ED6C (File and line number not available): kernel32.dll!BaseThreadInitThunk + 0x12 bytes
&&& 0x77D1377B (File and line number not available): ntdll.dll!RtlInitializeExceptionChain + 0xEF bytes
&&& 0x77D1374E (File and line number not available): ntdll.dll!RtlInitializeExceptionChain + 0xC2 bytes
&&& CD CD CD CD&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& ........ ........
---------- Block 4 at 0x00CA0348: 20 bytes ----------
& Call Stack:
&&& c:\users\ajaxhe\desktop\program\qt\practice\test_memoryleak\test_memoryleak\test_memoryleak.cpp (12): test_memoryLeak.exe!test_memoryLeak::test_memoryLeak + 0x10 bytes
&&& c:\users\ajaxhe\desktop\program\qt\practice\test_memoryleak\test_memoryleak\main.cpp (20): test_memoryLeak.exe!main + 0x17 bytes
&&& C:\Qt\4.7.4\src\winmain\qtmain_win.cpp (131): test_memoryLeak.exe!WinMain + 0x12 bytes
&&& f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (578): test_memoryLeak.exe!__tmainCRTStartup + 0x35 bytes
&&& f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (403): test_memoryLeak.exe!WinMainCRTStartup
&&& 0x76B9ED6C (File and line number not available): kernel32.dll!BaseThreadInitThunk + 0x12 bytes
&&& 0x77D1377B (File and line number not available): ntdll.dll!RtlInitializeExceptionChain + 0xEF bytes
&&& 0x77D1374E (File and line number not available): ntdll.dll!RtlInitializeExceptionChain + 0xC2 bytes
&&& 04 7C FB 00&&& 98 03 CA 00&&& E0 7B FB 00&&& 00 00 CD CD&&&& .|...... .{......
&&& 50 04 CA 00&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& P....... ........
Visual Leak Detector detected 2 memory leaks (96 bytes).
Largest number used: 260 bytes.
Total allocations: 260 bytes.
Visual Leak Detector is now exiting.
溢出位置与预期一致!
http://blog.csdn.net/itjobtxq/article/details/
Views(...) Comments()qt5 msvc 版本如何检查内存泄漏? - 知乎22被浏览1961分享邀请回答63 条评论分享收藏感谢收起Qt应用中检测内存泄露——VLD_Linux编程_Linux公社-Linux系统门户网站
你好,游客
Qt应用中检测内存泄露——VLD
来源:Linux社区&
本文简要描述一下在Qt应用中使用VLD来检测内存泄露。本次测试环境:QtCreator2.3 + Qt4.7.4-vs2008 + VS2008 Express.
1、下载并安装:VLD-2.2:&
假定安装到c:/dev/vld-2.2目录下。
注:vld最初发表在,这个版本太老了。检测不准,不能使用。
2、创建测试项目:使用QtCreator创建一个Qt GUI项目。
修改.pro文件,添加如下内容:
&&&&CONFIG(debug,&debug|release)&{&&
#&&&&&&&&DEFINES&+=&_DEBUG &&
&&&&&&&&#&vld&2.2&downloaded&from&/ &&
&&&&&&&&VLD_PATH&=&c:/dev/vld-2.2&&
&&&&&&&&INCLUDEPATH&+=&$VLD_PATH/include&&
&&&&&&&&LIBS&+=&-L$VLD_PATH/lib/Win32&-lvld&&
修改main.cpp文件,在main函数上面添加以下代码:
#ifdef&_DEBUG &&
#include&"vld.h" &&
3、进行测试:
测试1:在MainWindow的构造函数中添加一行代码:
new&QWidget(this);&//&不会泄露&&编译运行,在QtCreator的应用程序输出窗口中将会有类似下面的内容:
Visual&Leak&Detector&Version&2.2&installed.&&
Visual&Leak&Detector&Version&2.2&installed.&&
No&memory&leaks&detected.&&
Visual&Leak&Detector&is&now&exiting.&&以上表示没有发现内存泄露。
(初次运行时可能无法运行,这是因为找不到vld的dll文件。将C:\dev\vld-2.2\bin\Win32目录下的内容拷贝到PATH环境变量中所列的某个目录即可)
测试2:再添加一行代码:
new&QWidget(0);&&&再次编译运行,结果为:
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&(18):&test_vld_qt.exe!MainWindow::MainWindow&+&0x7&bytes&&
&&&&e:\works\test_vld_qt\main.cpp&(10):&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&&
&&&&8C&8A&40&00&&&&F8&81&F1&00&&&&68&8A&40&00&&&&00&00&CD&CD&&&&&..@.....&h.@.....&&
&&&&B0&82&F1&00&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&........&........&&
Visual&Leak&Detector&detected&1&memory&leak&(56&bytes).&&
Largest&number&used:&432&bytes.&&
Total&allocations:&432&bytes.&&
Visual&Leak&Detector&is&now&exiting.&&这次检测到了内存泄露。
小结:如上所示,使用vld检测内存泄露很容易,美中不足的是只能使用VC++编译器。尽管如此,我们也可以用它来在Win32下检测内存泄露,然后再使用其它编译器在其它平台上进行编译发布。
相关资讯 & & &
& (02月07日)
& (01/14/:20)
& (03月08日)
& (04/07/:36)
& (01/08/:52)
   同意评论声明
   发表
尊重网上道德,遵守中华人民共和国的各项有关法律法规
承担一切因您的行为而直接或间接导致的民事或刑事法律责任
本站管理人员有权保留或删除其管辖留言中的任意内容
本站有权在网站内转载或引用您的评论
参与本评论即表明您已经阅读并接受上述条款安全检查中...
请打开浏览器的javascript,然后刷新浏览器
< 浏览器安全检查中...
还剩 5 秒&}

我要回帖

更多关于 vs内存泄露检测工具 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信