Qt下内存泄漏的检测
第一步:新建setdebugnew.h 内容如下
#ifndef SETDEBUGNEW_H
#define SETDEBUGNEW_H
#ifdef _DEBUG
#define DEBUG_CLIENTBLOCK new( _CLIENT_BLOCK, __FILE__, __LINE__)
#else
#define DEBUG_CLIENTBLOCK
#endif
#define _CRTDBG_MAP_ALLOC
#include <crtdbg.h>
#ifdef _DEBUG
#define new DEBUG_CLIENTBLOCK
#endif
#endif // SETDEBUGNEW_H
第二部:导入新建的.h文件,在需要检测的cpp文件中包含setdebugnew.h头文件
第三部:main函数开头处加入以下代码
#ifdef _DEBUG
_CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
#endif