MFC检测内存泄露是最方便的,只要在需要检测的cpp文件开始包含
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
就可以了。这是通过重载new操作符,在DEBUG时可以在output中输出内存泄露的位置。
非MFC中检测内存泄漏需要加上
#ifdef _DEBUG
#define DEBUG_CLIENTBLOCK new( _CLIENT_BLOCK, __FILE__, __LINE__)
#else
#define DEBUG_CLIENTBLOCK
#endif // _DEBUG
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
#include <string.h>
#include <stdio.h>
#ifdef _DEBUG
#define new DEBUG_CLIENTBLOCK
#endif
并且在需要检测的地方(比较靠后的地方),加上
_CrtDumpMemoryLeaks();
或者只要在main的开始加上
_CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
如果只是是检测malloc产生的内存泄露,只需要加上
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
并且在需要检测的地方(比较靠后的地方),加上
_CrtDumpMemoryLeaks();
vc内存泄露检查
最新推荐文章于 2023-07-12 21:20:35 发布