最近在学习VC中MS CRT对内存泄漏的处理。自己总结了一下,写了一小段对new、malloc、realloc检测的代码:
#include "stdafx.h"
#include <Windows.h>
#include <crtdbg.h>
#ifdef _DEBUG
#define DEBUG_CLIENTBLOCK new( _CLIENT_BLOCK, __FILE__, __LINE__)
#else
#define DEBUG_CLIENTBLOCK
#endif
#include <crtdbg.h>
#ifdef _DEBUG
#define new DEBUG_CLIENTBLOCK
#define malloc(x) _malloc_dbg( x, _NORMAL_BLOCK, __FILE__, __LINE__)
#define realloc(x, y) _realloc_dbg( x, y, _NORMAL_BLOCK, __FILE__, __LINE__)
#endif
class A
{
public:
A() { a1 = new int; a2 = (char*)malloc(12);}
~A() { /*delete a1; free(a2);*/ }
int *a1;
char *a2;
};
int _tmain(int argc, _TCHAR* argv[])
{
_CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
int* p = new int;
//delete p;
char * y = (char*)malloc(10);
//free(y)