debug

本文介绍了使用多种方法和技术来检测C++中的内存泄漏问题,包括使用Lua脚本检查句柄泄漏、通过CRT库设置内存泄漏检测标志、利用PDH函数获取性能数据、通过对象计数和内存分配技巧定位泄漏源头、借助Mozilla提供的工具以及其他一些实用建议。

1, check handle leak.

Use Lua script to search the codes.

 

2, check memory leak.

If you use CRT, you could use:

_CrtSetDbgFlag(_CrtSetDbgFlag(_CRTDBG_REPORT_FLAG) | _CRTDBG_LEAK_CHECK_DF);
//_CrtSetBreakAlloc(182366);

// http://msdn.microsoft.com/en-US/library/e5ewb1h3(VS.80).aspx

 

3, get performance data.

We could use PDH functions. CPDHData

 

4, 注释代码隔离问题。

 

5,

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

 

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

 

7, OANOCACHE=1
http://msdn.microsoft.com/en-us/library/ms221105.aspx
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:

http://www.mozilla.org/performance/tools.html

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

#ifdef _DEBUG
    CMemoryState oldMemState, newMemState, diffMemState;
    oldMemState.Checkpoint();
#endif
     ...
 (被测试的代码)
     ...
#ifdef _DEBUG
    newMemState.Checkpoint();
   if(diffMemState.Difference(oldMemState, newMemState))    {
        TRACE(“Memory Leaked Here:\n\n" );
    }
#endif

转载于:https://www.cnblogs.com/lua5/archive/2009/08/21/1551529.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值