#ifdef _DEBUG #define DBG_NEW new ( _NORMAL_BLOCK , __FILE__ , __LINE__ ) // Replace _NORMAL_BLOCK with _CLIENT_BLOCK if you want the // allocations to be of _CLIENT_BLOCK type #else #define DBG_NEW new#endif
Q : 需要替换所有的源码,而且第三方库怎么办?
Q : 没有栈回溯信息,多层调用怎么查找源头信息?
A : Debug,在app入口断下后,在watch窗口输入"_crtBreakAlloc"(如果"Runtime Library"是"/MD",则还需要添加"{,,ucrtbased.dll}_crtBreakAlloc"),这个值应该是"-1",修改成detect到的memory leak的分配序号(如7中的{18}),当分配该大小的memory时就会断下来(注意,重跑后的条件要和第一次获取分配序号的一致)。
另外,在code中也可以直接指定:
_crtBreakAlloc = 18;
or :
_CrtSetBreakAlloc(18);
8. Test
1> code
#includestd ::tr1 ::shared_ptr <int > sp_nTest; void Test () { sp_nTest. reset( newint( 0x88)); //memory leakint* pnTest = newint( 0xCC); void* pMalloc = malloc( sizeof( int)); }
2> result
Detected memory leaks!
Dumping objects ->
d:\codes\vs2010\test\detectmemleak\console\test.cpp(14) : {65} normal block at 0x007B18A8, 4 bytes long.
Data: < > CD CD CD CD
{64} normal block at 0x007B4F90, 4 bytes long.Data: < > CC 00 00 00 Object dump complete.
红色部分是new出来的memory leak,可见并不会报出对应的code的行号