// Visual C++ 2010 DEBUG
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
class A
{
public:
int i;
A(){ i=9;}
};
class B
{
public:
short s;
B(){ s = 3;}
void* operator new(size_t size){ return ::operator new(size); }
void* operator new(size_t size, int block_type, const char *filename, int line) { return ::operator new(size, block_type, filename, line); }
};
#define new new( _NORMAL_BLOCK , __FILE__ , __LINE__ )
int main(int argc, char **argv)
{
_CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
_CrtSetReportMode( _CRT_ERROR, _CRTDBG_MODE_DEBUG );
void * p = malloc(100);
void * q = new A;
void * r = new B;
return 0;
}
输出:
Detected memory leaks!
Dumping objects ->
f:\desktop\leak\leak\main.cpp(31) : {86} normal block at 0x009714C0, 2 bytes long.
Data: < > 03 00
f:\desktop\leak\leak\main.cpp(30) : {85} normal block at 0x00971480, 4 bytes long.
Data: < > 09 00 00 00
f:\desktop\leak\leak\main.cpp(29) : {84} normal block at 0x009713E0, 100 bytes long.
Data: < > CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD
Object dump complete.
详见:http://msdn.microsoft.com/zh-cn/library/x98tx3cf(v=vs.100).aspx