MFC程序内存泄露检查

CMemoryState可以帮助我们检测程序中的内存泄漏,具体定义为:

CMemoryState provides a convenient way to detect memory leaks in your program. A “memory leak” occurs when memory for an object is allocated on the heap but not deallocated when it is no longer required. Such memory leaks can eventually lead to out-of-memory errors. There are several ways to allocate and deallocate memory in your program:

  • Using the malloc/free family of functions from the run-time library.
  • Using the Windows API memory management functions, LocalAlloc/LocalFree andGlobalAlloc/GlobalFree.
  • Using the C++ new and delete operators.

The CMemoryState diagnostics only help detect memory leaks caused when memory allocated using thenew operator is not deallocated using delete. The other two groups of memory-management functions are for non-C++ programs, and mixing them withnew and delete in the same program is not recommended. An additional macro,DEBUG_NEW, is provided to replace the new operator when you need file and line-number tracking of memory allocations.DEBUG_NEW is used whenever you would normally use the new operator.

As with other diagnostics, the CMemoryState diagnostics are only available in debug versions of your program. A debug version must have the_DEBUG constant defined.

If you suspect your program has a memory leak, you can use the Checkpoint, Difference, and DumpStatistics functions to discover the difference between the memory state (objects allocated) at two different points in program execution. This information can be useful in determining whether a function is cleaning up all the objects it allocates.

具体实现方法为:

APP定义成员变量

#ifdef _DEBUG   //一定要在debug模式下搞啊

       CMemoryState m_StPt, m_EndPt, m_Diff;

#endif //_DEBU

 

InitInstance函数中添加

       if (! AfxOleInit())

          return FALSE;

#ifdef _DEBUG //一定要在debug模式下搞啊

      m_StPt.Checkpoint();

#endif //_DEBUG

 

ExitInstance函数中添加

int CTestApp::ExitInstance()

{

#ifdef _DEBUG//一定要在debug 模式下搞啊

      m_EndPt.Checkpoint();

      if (m_Diff.Difference(m_StPt, m_EndPt))

      {

             TRACE("\n-------- Detected memory leaks. DUMP(DumpStatistics) Start --------\n");

             m_Diff.DumpStatistics();

             TRACE("-------- Detected memory leaks. DUMP(DumpStatistics) End --------\n");

             TRACE("\n-------- Detected memory leaks. DUMP(DumpAllObjectsSince) Start --------\n");

             m_Diff.DumpAllObjectsSince();

             TRACE("-------- Detected memory leaks. DUMP(DumpAllObjectsSince) End --------\n\n");

      }

      else

      {

             TRACE("\n-------- No detected memory leaks. OK !! --------\n\n");

      }

#endif //_DEBUG

      return CWinApp::ExitInstance();

}

 

完毕

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值