首先,我們需要知道C運行庫的Debug版本提供了許多檢測功能,使得我們更容易的Debug程式。在MSDN中有專門的章節講這個,叫做Debug Routines,建議大家先看看裡面的內容吧。
我們會用到裡面很重要的幾個函數。其中最重要的是 _CrtDumpMemoryLeaks();自己看MSDN裡的説明吧。使用這個函數,需要包含標頭檔crtdbg.h
該函數只在Debug版本才有用,當在調試器下運行程式時,_CrtDumpMemoryLeaks 將在「Output(輸出)」視窗中顯示記憶體洩漏資訊.寫段代碼試驗一下吧,如下:
檢測記憶體洩露版本一:

 

#include "stdafx.h"
#include <crtdbg.h>
int _tmain(int argc, _TCHAR* argv[])
{
int* p = new int();
_CrtDumpMemoryLeaks();
return 0;
}
運行後,在Output(輸出)視窗,顯示了如下的資訊:

 

Detected memory leaks!
Dumping objects ->
{112} normal block at 0x003AA770, 4 bytes long.
Data: < > 00 00 00 00
Object dump complete.
但是這個只是告訴我們程式有記憶體洩露,到底在哪洩露了一眼看不出來啊。
看我們的檢測記憶體洩露版本二:

 

#include "stdafx.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
int _tmain(int argc, _TCHAR* argv[])
{
int* p = new int();
_CrtDumpMemoryLeaks();
return 0;
}
該程式定義了幾個宏,通過宏將Debug版本下的new給替換了,新的new記錄下了調用new時的檔案名和程式碼.運行後,可以看到如下的結果:

 

Detected memory leaks!
Dumping objects ->
d:\code\consoletest\consoletest.cpp(21) : {112} client block at 0x003A38B0, subtype 0, 4 bytes long.
Data: < > 00 00 00 00
Object dump complete.
呵呵,已經和MFC程式的效果一樣了,但是等一等。看下如下的代碼吧:

 

int _tmain(int argc, _TCHAR* argv[])
{
int* p = new int();
_CrtDumpMemoryLeaks();
delete p;
return 0;
}
運行後可以發現我們刪除了指標,但是它仍然報記憶體洩露。所以可以想像,每調用一次new,程式內部都會將該調用記錄下來,類似于有個數組記錄,假如delete了,那麼就將其從陣列中刪除,而_CrtDumpMemoryLeaks()就是把這個陣列當前的狀態列印出來。
所以除了在必要的時候Dump出記憶體資訊外,最重要的就是在程式退出的時候需要掉用一次_CrtDumpMemoryLeaks();
假如程式有不止一個出口,那麼我們就需要在多個地方都調用該函數。
更進一步,假如程式在類的析構函數裡刪除指標,怎麼辦?例如:

 

#include "stdafx.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
class Test
{
public:
Test() { _p = new int(); }
~Test() { delete _p; }
int* _p;
};
int _tmain(int argc, _TCHAR* argv[])
{
int* p = new int();
delete p;
Test t;
_CrtDumpMemoryLeaks();
return 0;
}
可以看到析構函數在程式退出的時候才調用,明明沒有記憶體洩露,但是這樣的寫法還是報了。
如何改進呢,看檢測記憶體洩露版本三:

 

#include "stdafx.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
class Test
{
public:
Test() { _p = new int(); }
~Test() { delete _p; }
int* _p;
};
int _tmain(int argc, _TCHAR* argv[])
{
_CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
int* p = new int();
delete p;
Test t;
return 0;
}
_CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
該語句在程式退出時自動調用 _CrtDumpMemoryLeaks。必須同時設置 _CRTDBG_ALLOC_MEM_DF 和 _CRTDBG_LEAK_CHECK_DF.
這樣,該版本已經達到了MFC一樣的效果了,但是我覺得光這樣還不夠,因為我們只是在Output視窗中輸出資訊,對開發人員的提醒還不明顯,經常會被遺漏,而且很多人就算發現了記憶體洩露,但是不好修復,不會嚴重影響到程式外在表現,都不會修復。怎麼樣能讓開發人員主動的修復記憶體洩露的問題呢?記得曾經和人配合寫程式,我的函數參數有要求,不能為空,但是別人老是傳空值,沒辦法了,只好在函數開始驗證函數參數,給他assert住,這樣程式運行時老是不停的彈出assert,偵錯工具那個煩壓,最後其他程式師煩了,就把這個問題給改好了,輸入參數就正確了。所以我覺得咱要讓程式師主動去做一件事,首先要讓他覺得做這個事是能減輕自己負擔,讓自己工作輕鬆的。呵呵,那咱們也這樣,當程式退出時,檢測到記憶體洩露就讓程式提示出來。
看檢測記憶體洩露版本四:

 

#include "stdafx.h"
#include <assert.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
void Exit()
{
int i = _CrtDumpMemoryLeaks();
assert( i == 0);
}
int _tmain(int argc, _TCHAR* argv[])
{
atexit(Exit);
int* p = new int();
return 0;
}
該版本會在程式退出時檢查記憶體洩露,假如存在就會彈出提示對話方塊.
atexit(Exit);設置了在程式退出時執行Exit()函數。
Exit()函數中,假如存在記憶體洩露,_CrtDumpMemoryLeaks()會返回非0值,就會被assert住了。

 

到這個版本已經達到可以使用的程度了。但是我們還可以做些改進,因為真要準確的檢測到代碼中所有的記憶體洩露,需要把代碼中的#define......拷貝到所有使用new的檔中。不可能每個檔都拷貝這麼多代碼,所以我們可以將他提取出來,放在一個檔中,比如我是放在KDetectMemoryLeak.h中,該檔內容如下:

 

#pragma once
#ifdef _DEBUG
#define DEBUG_CLIENTBLOCK new( _CLIENT_BLOCK, __FILE__, __LINE__)
#else
#define DEBUG_CLIENTBLOCK
#endif
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
#ifdef _DEBUG
#define new DEBUG_CLIENTBLOCK
#endif
然後將KDetectMemoryLeak.h包含在專案的通用檔中,例如用VS建的專案就將其包含在stdafx.h中。或者我自己建的一個Common.h檔中,該檔包含一些通用的,基本所有檔都會用到的代碼東東。

 

好了,到現在,檢測記憶體洩露總算完成了,而且他還能定位到到底是代碼中哪個檔,哪行出現了記憶體洩露。




 

引文來源 C++ 記憶體洩露的檢測 - Augusdi的專欄 - CSDN博客
arrow
arrow
    全站熱搜

    戮克 發表在 痞客邦 留言(0) 人氣()