VS2005内存泄漏检测方法

  • 格式:doc
  • 大小:76.50 KB
  • 文档页数:5

下载文档原格式

  / 5
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

VS2005内存泄漏检测方法

2010-03-09 09:13 247人阅读评论(0) 收藏举报VS2005内存泄漏检测方法

非MFC程序可以用以下方法检测内存泄露:

1.程序开始包含如下定义:

view plaincopy to clipboardprint?

1. #ifdef _DEBUG

2. #define DEBUG_CLIENTBLOCK new( _CLIENT_BLOCK, __FILE__, __LINE__)

3. #else

4. #define DEBUG_CLIENTBLOCK

5. #endif // _DEBUG

6. #define _CRTDBG_MAP_ALLOC

7. #include

8. #include

9. #ifdef _DEBUG

10. #define new DEBUG_CLIENTBLOCK

11. #endif // _DEBUG

2.程序中添加下面的函数:

view plaincopy to clipboardprint?

1. _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF|_CRTDBG_LEAK_CHECK_DF);

1. #ifdef _DEBUG

2. protected:

3. CMemoryState m_msOld, m_msNew, m_msDiff;

4. #endif // _DEBUG

1. #ifdef _DEBUG

2. m_msOld.Checkpoint();

3. #endif // _DEBUG

4.

5.

3.在 CMyApp::ExitInstance() 中添加如下代码:

view plaincopy to clipboardprint?

1. #ifdef _DEBUG

2. m_msNew.Checkpoint();

3. if (m_msDiff.Difference(m_msOld, m_msNew))

4. {

5. afxDump<<"/nMemory Leaked :/n";

6. m_msDiff.DumpStatistics();

7. afxDump<<"Dump Complete !/n/n";

8. }

9. #endif // _DEBUG

0 bytes in 0 Client Blocks.

Largest number used: 8825 bytes.

Total allocations: 47506 bytes.

Dump Complete !

Detected memory leaks!

Dumping objects ->

g:/programs/chat/chatdlg.cpp(120) : {118} normal block at 0x00D98150, 8 bytes long. Data: < > A8 7F D9 00 01 00 00 00

Object dump complete.