C++内存泄露检测(Visual studio 2010)

方法1:使用crtdbg.h

1、在程序头部添加:

#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>

注意:放在#include "stdafx.h"之后,否则会报错。

2、在程序中添加语句:

_CrtDumpMemoryLeaks();

Example:

#include "stdafx.h"
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
#include <iostream>
using namespace std;
class MyClass{};
int _tmain(int argc, _TCHAR* argv[])
{
     for(int i=0;i<3;i++){
	 MyClass *mc=new MyClass;
	 cout<<mc<<endl;
     }
     _CrtDumpMemoryLeaks();
     system("pause");
     return 0;
}

DOS输出:

Console输出:

Detected memory leaks!
Dumping objects ->
{190} normal block at 0x003A6F68, 1 bytes long.
 Data: < > CD 
{188} normal block at 0x003A6F28, 1 bytes long.
 Data: < > CD 
{163} normal block at 0x003A6B58, 1 bytes long.
 Data: < > CD 
Object dump complete.

 {190}:内存分配编号

normal block:块类型(普通、客户端或 CRT);”普通块“是由程序分配的普通内存,“客户端块”是由 MFC 程序用于需要析构函数的对象的特殊类型内存块,“CRT 块”是由 CRT 库为自己使用而分配的内存块。

0x003A6F68:十六进制形式的内存位置

1 bytes long:以字节为单位的块大小

Data: < > CD:前 16 字节的内容(亦为十六进制)。

方法2:使用visual leak detector

1、下载vld:http://vld.codeplex.com/releases/view/82311

2、配置

①新建项目

②点击:视图---->属性管理器

双击打开Microsoft.Cpp.Win32.user

④选择VC++ 目录,在"包含目录" 选择安装路径visual leak detector\include文件夹,在"库目录" 选择安装路径visual leak detector\lib\Win32文件夹(如果64位机器选择win64文件夹)

⑤重启 vs2010

3、测试

注意:需添加头文件#include"vld.h"

#include "stdafx.h"
#include "vld.h"
#include <iostream>
using namespace std;
class MyClass{};
int _tmain(int argc, _TCHAR* argv[])
{
	for(int i=0;i<3;i++){
		MyClass *mc=new MyClass;
		cout<<mc<<endl;
	}
	system("pause");
	return 0;
}

DOS输出:

Console输出:

WARNING: Visual Leak Detector detected memory leaks!
---------- Block 1 at 0x00666B58: 1 bytes ----------
  Call Stack:
    c:\users\administrator\documents\visual studio 2010\projects\test\test\test.cpp (12): Test.exe!wmain + 0x7 bytes
    f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (552): Test.exe!__tmainCRTStartup + 0x19 bytes
    f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (371): Test.exe!wmainCRTStartup
    0x76D0ED6C (File and line number not available): kernel32.dll!BaseThreadInitThunk + 0x12 bytes
    0x77D4377B (File and line number not available): ntdll.dll!RtlInitializeExceptionChain + 0xEF bytes
    0x77D4374E (File and line number not available): ntdll.dll!RtlInitializeExceptionChain + 0xC2 bytes
  Data:
    CD                                                          ........ ........


---------- Block 2 at 0x00666F28: 1 bytes ----------
  Call Stack:
    c:\users\administrator\documents\visual studio 2010\projects\test\test\test.cpp (12): Test.exe!wmain + 0x7 bytes
    f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (552): Test.exe!__tmainCRTStartup + 0x19 bytes
    f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (371): Test.exe!wmainCRTStartup
    0x76D0ED6C (File and line number not available): kernel32.dll!BaseThreadInitThunk + 0x12 bytes
    0x77D4377B (File and line number not available): ntdll.dll!RtlInitializeExceptionChain + 0xEF bytes
    0x77D4374E (File and line number not available): ntdll.dll!RtlInitializeExceptionChain + 0xC2 bytes
  Data:
    CD                                                           ........ ........


---------- Block 3 at 0x00666F68: 1 bytes ----------
  Call Stack:
    c:\users\administrator\documents\visual studio 2010\projects\test\test\test.cpp (12): Test.exe!wmain + 0x7 bytes
    f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (552): Test.exe!__tmainCRTStartup + 0x19 bytes
    f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (371): Test.exe!wmainCRTStartup
    0x76D0ED6C (File and line number not available): kernel32.dll!BaseThreadInitThunk + 0x12 bytes
    0x77D4377B (File and line number not available): ntdll.dll!RtlInitializeExceptionChain + 0xEF bytes
    0x77D4374E (File and line number not available): ntdll.dll!RtlInitializeExceptionChain + 0xC2 bytes
  Data:
    CD                                                           ........ ........


Visual Leak Detector detected 3 memory leaks (111 bytes).
Largest number used: 111 bytes.
Total allocations: 111 bytes.
Visual Leak Detector is now exiting.

参考

1、C/C++内存泄漏及检测:http://www.cnblogs.com/skynet/archive/2011/02/20/1959162.html

2、Using Visual Leak Detector:http://vld.codeplex.com/wikipage?title=Using%20Visual%20Leak%20Detector&referringTitle=Documentation

3、vs2010 visual leak detector 配置方法 和 静态指针的释放:http://blog.csdn.net/frankiewang008/article/details/7975326

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值