来源 https://www.zhihu.com/question/63946754/answer/214762551
linux+gcc用valgrind,windows+msvc用vs调试功能。
如果是在windows上跑mingw就坑爹了,要么买专门的商业检测软件,要么用代码侵入的方式,重载operator new,接入内存泄漏检测代码
只要每一个cpp在展开后都能看见这几行(https://github.com/vczh-libraries/Vlpp/blob/master/Source/Basic.h):
#ifdef VCZH_CHECK_MEMORY_LEAKS
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
#define VCZH_CHECK_MEMORY_LEAKS_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__)
#define new VCZH_CHECK_MEMORY_LEAKS_NEW
#endif
在程序退出的时候,调用_CrtDumpMemoryLeaks函数,Visual Studio的output窗口就会打印所有没有释放的东西(包括全局变量——所以不要在全局变量里面使用非指针或数字类型),双击可以跳进代码。
我都配置成debug模式会检查,每次运行的时候都在检查,有时候我都忘记他在检查了然后突然就蹦出来了消息说我内存泄漏了,然后马上改。所以我的程序绝对没有内存泄漏(逃
如果是Windows的话可以利用在DEBUG环境下检查
#ifdef _WIN32
#include <crtdbg.h>
#ifdef _DEBUG
#define new new(_NORMAL_BLOCK,__FILE__,__LINE__)
#endif
#endif
#ifdef _WIN32
_CrtSetDbgFlag(_CrtSetDbgFlag(_CRTDBG_REPORT_FLAG)|_CRTDBG_LEAK_CHECK_DF);
#endif
原文:https://www.cnblogs.com/lsgxeva/p/12840768.html