Visual Studio如何检查内存泄漏

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/winter_wu_1998/article/details/84202654

https://msdn.microsoft.com/zh-cn/library/x98tx3cf.aspx

Clion中有专门检查内存泄漏的选项,而VS中却没有。但是我们可以通过下面的方法检查:

  • 在程序的开头加上下面三行:
#define CRTDBG_MAP_ALLOC  
#include <stdlib.h>    
#include <crtdbg.h>
  • 在main函数结尾,也就是return 0 的上一行加上下面代码:
_CrtDumpMemoryLeaks();
  • 通过debug模式运行,不设断点,直接跑完程序,在输出界面会显示内存泄漏的内容,例如:
Dumping objects ->
{205} normal block at 0x01362090, 8 bytes long.
 Data: < 76     > AC 37 36 01 00 00 00 00 
  • 为了定位出错点,使用下面的代码,会在内存泄漏点触发异常:
_CrtSetBreakAlloc(205);

猜你喜欢

转载自blog.csdn.net/winter_wu_1998/article/details/84202654
今日推荐