一个用于C/C++内存检测的工具
Visual Leak Detector - Enhanced Memory Leak Detection for Visual C++
下载并解压,将其保存到一个Visual Studio可以方便访问的目录下
(我用的是Visual Studio 2008)
由于不太熟悉VC2008的环境,所以查了一下怎么添加项目的include目录和library目录
reference the library headers in Project Properties->C/C++->General->Additional Include Directories
reference the .lib in Project Properties->Linker->Input
see http://msdn.microsoft.com/en-us/library/ba1z7822.aspx for more details
编写测试程序:
#include "vld.h"
#include <iostream>
using namespace std;
int* getInt() {
int* p = new int(3);
return p;
}
int main(int argc,char** argv) {
int* pInt = getInt();
cout << *pInt << endl;
//delete pInt;
return 0;
}
直接看getInt()函数,在main()方法中注释掉"delete pInt;"语句,点击“调试”->“启动调试”,可以调试信息的输出:
Visual Leak Detector Version 1.0 installed (multithreaded DLL).
WARNING: Visual Leak Detector detected memory leaks!
---------- Block 120 at 0x00398F20: 4 bytes ----------
Call Stack:
f:\dd\vctools\crt_bld\self_x86\crt\src\dbgmalloc.c (56): malloc
d:\mutlithread\mutlithread\dead_lock.cpp (6): getInt
d:\mutlithread\mutlithread\dead_lock.cpp (12): main
f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (582): __tmainCRTStartup
f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (399): mainCRTStartup
0x7C81776F (File and line number not available): RegisterWaitForInputIdle
Data:
03 00 00 00 ........ ........
Visual Leak Detector detected 1 memory leak.
“MutliThread.exe”: 已卸载“C:\WINDOWS\system32\dbghelp.dll”
“MutliThread.exe”: 已卸载“C:\WINDOWS\system32\version.dll”
Visual Leak Detector is now exiting.
调试过程中检测到一处内存泄露,可以双击错误,并定位到错误出处。
拓展阅读:
内存泄露检测工具比较
内存泄露检测工具