Debug step by step(1) Memeory leak问题调试常用手段

1, check handle leak.

Use Lua script to search the codes.

2, check memory leak.

If you use CRT, you could use:



3, get performance data.

We could use PDH functions. CPDHData

4, 注释代码隔离问题。


1. 加一个对象计数器, 哪种对象一直增加, 就是它了.
2. 申请比较多的类, 轮流内部增加一个 char buffer[65536], 看看内存增加是否加快, 很快就可以找出了.

6, VLD boundchecker ADPlus WinDbg SysInternals-Tools (handle)

For example, if the application allocates a BSTR and frees it, the free block of memory is put into the BSTR cache by Automation. If the application then allocates another BSTR, it can get the free block from the cache. If the second BSTR allocation is not freed, IMallocSpy will attribute the leak to the first allocation of the BSTR. You can determine the correct source of the leak (the second allocation) by disabling the BSTR caching using the debug version of Oleaut32.dll, and by setting the environment variable OANOCACHE=1 before running the application.

8, some tools Mozilla uses:

9, 启用或禁用内存诊断可以调用全局函数 AfxEnableMemoryTracking()

#ifdef _DEBUG
CMemoryState oldMemState, newMemState, diffMemState;


#ifdef _DEBUG
if(diffMemState.Difference(oldMemState, newMemState)) {
TRACE(“Memory Leaked Here:\n\n” );


《Debug step by step(1) Memeory leak问题调试常用手段》有2个想法