1. 程式人生 > >C++中基於Crt的記憶體洩漏檢測

C++中基於Crt的記憶體洩漏檢測

儘管這個概念已經讓人說濫了 ,還是想簡單記錄一下, 以備以後查詢。

#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


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 <new>

#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


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 <crtdbg.h>
#ifdef _DEBUG
#define new DEBUG_CLIENTBLOCK
#endif

#include <map>你應該把 #include <map>放到 巨集定義的前面。

(7) 如果你在巨集 #define new DEBUG_CLIENTBLOCK 之後再宣告或定義 operator new函式, 都會因為巨集替代而編譯失敗。
而STL的xdebug檔案恰恰申明瞭operator new函式, 所以請確保new的替代巨集放在所有include標頭檔案的最後, 尤其要放在STL標頭檔案的後面。
//MyClass.cpp#include "myclass.h"
#include <map>
#include <algorithm>

#ifdef _DEBUG
#define new DEBUG_CLIENTBLOCK
#endif

MyClass::MyClass()
{
    char* p = new char('a');
}
(8)如果你覺得上面的這種new替代巨集分散在各個CPP裡太麻煩, 想把所有的東西放到一個通用標頭檔案裡,請參考下面定義的方式:
//MemLeakChecker.h #include <map>
#include <algorithm>
//other STL file
#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 
(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;
}(10) 其實知道了原理, 自己寫一套C++記憶體洩露檢測也不難, 主要是過載operator new和operator delete, 可以把每次記憶體分配情況都記錄在一個Map裡, delete時刪除記錄, 最後程式退出時把map裡沒有delete的打印出來。 當然我們知道Crt在實現new時一般實際上調的是malloc, 而malloc可能又是調HeapAlloc,而HeapAlloc可能又是呼叫RtlAllocateHeap, 所以理論上我們可以在這些函式的任意一層攔截和記錄。但是如果你要實現自己的跨平臺記憶體洩露檢測,還是過載operator new吧。 posted on 2013-02-25 22:13 Richard Wei 閱讀(6055) 評論(2)  編輯 收藏 引用 所屬分類: C++