盡管這個概念已經讓人說濫了 ,還是想簡單記錄一下, 以備以后查詢。
#ifdef _DEBUG
#define DEBUG_CLIENTBLOCK new( _CLIENT_BLOCK, __FILE__, __LINE__)
#else
#define DEBUG_CLIENTBLOCK
#endif
#define _CRTDBG_MAP_ALLOC
#include
#ifdef _DEBUG
#define new DEBUG_CLIENTBLOCK
#endif
int _tmain(int argc, _TCHAR* argv[])
{
char* p = new char();
char* pp = new char[10];
char* ppp = (char*)malloc(10);
_CrtDumpMemoryLeaks();
return 0;
}
主要原理是運用Crt 的內存調試功能, 通過宏替代默認的operator new, 讓它被下面版本替代:
void *__CRTDECL operator new(
size_t cb,
int nBlockUse,
const char * szFileName,
int nLine
)
_THROW1(_STD bad_alloc)
{
/* _nh_malloc_dbg already calls _heap_alloc_dbg in a loop and calls _callnewh
if the allocation fails. If _callnewh returns (very likely because no
new handlers have been installed by the user), _nh_malloc_dbg returns NULL.
*/
void *res = _nh_malloc_dbg( cb, 1, nBlockUse, szFileName, nLine );
RTCCALLBACK(_RTC_Allocate_hook, (res, cb, 0));
/* if the allocation fails, we throw std::bad_alloc */
if (res == 0)
{
static const std::bad_alloc nomem;
_RAISE(nomem);
}
return res;
}
這樣Crt會把此次分配內存的文件名和行號以及大小等記錄下來,最后當調用用_CrtDumpMemoryLeaks(); 時如果還沒釋放就會打印出來。
結果如下:
Detected memory leaks!
Dumping objects ->
f:\test\memleakchecker\memleakchecker\memleakchecker.cpp(23) : {108} normal block at 0x0003A1A8, 10 bytes long.
Data: < > CD CD CD CD CD CD CD CD CD CD
f:\test\memleakchecker\memleakchecker\memleakchecker.cpp(22) : {107} client block at 0x0003A160, subtype 0, 10 bytes long.
Data: < > CD CD CD CD CD CD CD CD CD CD
f:\test\memleakchecker\memleakchecker\memleakchecker.cpp(21) : {106} client block at 0x0003A120, subtype 0, 1 bytes long.
Data: < > 00
Object dump complete.
下面是一些注意事項:
(1) #define _CRTDBG_MAP_ALLOC 的作用
如果不定義這個宏, C方式的malloc泄露不會被記錄下來。
(2)數字{108} {107}的作用
表示第幾次分配, 你可以通過_CrtSetBreakAlloc程序運行到預定次數時暫停 ,比如
int _tmain(int argc, _TCHAR* argv[])
{
_CrtSetBreakAlloc(108);
char* p = new char();
char* pp = new char[10];
char* ppp = (char*)malloc(10);
_CrtDumpMemoryLeaks();
return 0;
}
(3)如果程序有多個出口或是有涉及到全局變量, 可以通過_CrtSetDbgFlag 設置標志讓程序退出時自動打印泄露 , 比如
int _tmain(int argc, _TCHAR* argv[])
{
_CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
char* p = new char();
char* pp = new char[10];
char* ppp = (char*)malloc(10);
return 0;
}
(4)我們知道宏替代是最粗暴的方式, 所以盡量把下面new的替代宏放到每個Cpp里而不是放到一個通用的頭文件中, 實際上MFC也是這么做的
#ifdef _DEBUG
#define new DEBUG_CLIENTBLOCK
#endif
(5)上面的operator new只能照顧到最普通的new, 實際上operator new是有任意多種重載方式, 只需要確保第一個參數是表示大小。 比如下面的placement new就會編譯失敗, 因為宏替代后格式不符合要求了, 所以如果你的CPP用了非標準的new, 就不要加入new的檢測宏了。
#include
#ifdef _DEBUG
#define DEBUG_CLIENTBLOCK new( _CLIENT_BLOCK, __FILE__, __LINE__)
#else
#define DEBUG_CLIENTBLOCK
#endif
#define _CRTDBG_MAP_ALLOC
#include
#ifdef _DEBUG
#define new DEBUG_CLIENTBLOCK
#endif
int _tmain(int argc, _TCHAR* argv[])
{
_CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
char* p = new char();
char* pp = new char[10];
char* ppp = (char*)malloc(10);
char d;
char* p1 = new(&d) char('a');
return 0;
}
(6)因為STL里map內的tree用到了placement new, 所以如果你這樣用會編譯失敗:
#ifdef _DEBUG
#define DEBUG_CLIENTBLOCK new( _CLIENT_BLOCK, __FILE__, __LINE__)
#else
#define DEBUG_CLIENTBLOCK
#endif
#define _CRTDBG_MAP_ALLOC
#include
#ifdef _DEBUG
#define new DEBUG_CLIENTBLOCK
#endif
#include
你應該把 #include
//MyClass.cpp
#include "myclass.h"
#include
(8)如果你覺得上面的這種new替代宏分散在各個CPP里太麻煩, 想把所有的東西放到一個通用頭文件里,請參考下面定義的方式:
//MemLeakChecker.h
#include
(9)簡單判斷某個獨立函數有沒有內存泄露可以用下面的方法:
class DbgMemLeak
{
_CrtMemState m_checkpoint;
public:
explicit DbgMemLeak()
{
_CrtMemCheckpoint(&m_checkpoint);
};
~DbgMemLeak()
{
_CrtMemState checkpoint;
_CrtMemCheckpoint(&checkpoint);
_CrtMemState diff;
_CrtMemDifference(&diff, &m_checkpoint, &checkpoint);
_CrtMemDumpStatistics(&diff);
_CrtMemDumpAllObjectsSince(&diff);
};
};
int _tmain(int argc, _TCHAR* argv[])
{
DbgMemLeak check;
{
char* p = new char();
char* pp = new char[10];
char* ppp = (char*)malloc(10);
}
return 0;
}