c_cpp [C ++]我们如何在程序中检测内存泄漏

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp [C ++]我们如何在程序中检测内存泄漏相关的知识,希望对你有一定的参考价值。

/*
We know that memory is always at a premium. So as a programmer we have to to ensure minimum memory consumption as possible and necessary to avoid memory leaks from the program. How can we detect memory leaks in our program.

MFC provides a structure CMemoryState to detect memory leaks in our program. This diagnostics method only help to detect memory leaks caused when memory allocated using the new operator is not deallocated using delete.

CMemoryState will not address the leaks caused by malloc , LocalAlloc & GlobalAlloc.
*/

void DetectMemoryLeak()
{
   CMemoryState oldMemState, newMemState, diffMemState;
   oldMemState.Checkpoint();
   /* new memory is allocated in the heap */
   int *p = new int;
   /* heap memory is not yet deallocated */
   newMemState.Checkpoint();
   if(diffMemState.Difference(oldMemState,newMemState))
   {
      AfxMessageBox(_T("Detects Memory Leak"));
   }
}

以上是关于c_cpp [C ++]我们如何在程序中检测内存泄漏的主要内容,如果未能解决你的问题,请参考以下文章

您如何检测/避免(非托管)代码中的内存泄漏? [关闭]

什么是内存泄漏?

如何在 64 位应用程序中使用 32 位指针?

C和内存中的资源保护

在Objective C中,我们如何将脸部模糊成圆形/圆形?

如何定位Android NDK开发中遇到的错误