Memory Leak Detection Enabling in VS.

 

//The #include statements must be in the order shown here. If you change the order, the functions you use may not //work properly.

 

 

With _CRTDBG_MAP_ALLOC defined, the display shows:

  • The memory allocation number (inside the curly braces).

  • The block type, which is normal, client, or CRT.

  • The memory location in hexadecimal form.

  • The size of the block in bytes.

  • The contents of the first 16 bytes, also in hexadecimal form.

     

 

 

#define _CRTDBG_MAP_ALLOC 

#include <stdlib.h>

#include <crtdbg.h>

 

 

 

#ifdef _DEBUG

#define DEBUG_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__)

#define new DEBUG_NEW

#endif

 

int _tmain(int argc, _TCHAR* argv[])

{

 

// dump memory leak information

_CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );

 

char * a = (char*)malloc(5);

char *e = new char[10];

return 0;

}

 

 

//Result

 

Detected memory leaks!

Dumping objects ->

e:/vsproject/unittest/unittest.cpp(42) : {187} normal block at 0x0003D538, 10 bytes long.

 Data: <          > CD CD CD CD CD CD CD CD CD CD 

e:/vsproject/unittest/unittest.cpp(41) : {186} normal block at 0x0003D4F0, 5 bytes long.

 Data: <     > CD CD CD CD CD 

Object dump complete.

 

 

By default, _CrtDumpMemoryLeaks dumps memory leak information to the Debug pane of the Output window, as described previously. You can reset this to dump to another location using _CrtSetReportMode

 

 

PS:

Valgrind 

Valgrind是一个GPL的软件,用于Linux(For x86, amd64 and ppc32)程序的内存调试和代码剖析。你可以在它的环境中运行你的程序来监视内存的使用情况,比如C 语言中的malloc和free或者 C++中的new和 delete。使用Valgrind的工具包,你可以自动的检测许多内存管理和线程的bug,避免花费太多的时间在bug寻找上,使得你的程序更加稳固。

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值