在项目开发过程中,难免会出现内存泄露的情况,下面的方法可以帮助你快速找到哪里发生了内存泄露:

1. 在头文件中,添加如下代码:


#ifdef _DEBUG
#include 
<stdlib.h>
#include 
<crtdbg.h>
#endif


2. 在函数退出的地方,添加如下代码:

 

#ifdef _DEBUG
    _CrtDumpMemoryLeaks();
#endif

F5调试程序,将会输出如下类似信息:

{49} normal block at 0x00394F98, 8 bytes long.
 Data: <        > CD CD CD CD CD CD CD CD

3. 添加如下代码,将会在输出的信息中显示文件名及行号:


#define new new(_NORMAL_BLOCK, __FILE__, __LINE__)


dtest.cpp(13) : {49} normal block at 0x00394F98, 8 bytes long.
 Data: <        > CD CD CD CD CD CD CD CD
 

4. 如何没有显示文件名及行号,也可以在程序入口添加添加如下代码:

#ifdef _DEBUG // 检查是否有内存泄露
    _CrtSetBreakAlloc(49);
#endif 


 

相关文章:

  • 2021-08-04
  • 2021-05-19
  • 2022-12-23
  • 2022-12-23
  • 2021-12-07
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-01-03
  • 2021-07-27
  • 2021-11-30
  • 2021-11-18
  • 2021-12-14
  • 2021-10-25
相关资源
相似解决方案