在用vc時(shí),利用AppWizard會(huì )產(chǎn)生如下代碼:
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
對于
#define new DEBUG_NEW
首先看msdn的解釋?zhuān)?/p>
Assists in finding memory leaks. You can use DEBUG_NEW everywhere in your program that you would ordinarily use the new operator to allocate heap storage.
In debug mode (when the _DEBUG symbol is defined), DEBUG_NEW keeps track of the filename and line number for each object that it allocates. Then, when you use the CMemoryState::DumpAllObjectsSince member function, each object allocated with DEBUG_NEW is shown with the filename and line number where it was allocated.
To use DEBUG_NEW, insert the following directive into your source files:
#define new DEBUG_NEW
Once you insert this directive, the preprocessor will insert DEBUG_NEW wherever you use new, and MFC does the rest. When you compile a release version of your program, DEBUG_NEW resolves to a simple new operation, and the filename and line number information is not generated.
再查看定義:
#ifdef _DEBUG
void* AFX_CDECL operator new(size_t nSize, LPCSTR lpszFileName, int nLine);
#define DEBUG_NEW new(THIS_FILE, __LINE__)
#else
#define DEBUG_NEW new
#endif
這樣就很清楚了,當在debug模式下時(shí),我們分配內存時(shí)的new被替換成DEBUG_NEW,而這個(gè)DEBUG_NEW不僅要傳入內存塊的大小,還要傳入源文件名和行號,這就有個(gè)好處,即當發(fā)生內存泄漏時(shí),我們可以在調試模式下定位到該問(wèn)題代碼處。若刪掉該句,就不能進(jìn)行定位了。而在release版本下的new就是簡(jiǎn)單的new,并不會(huì )傳入文件名和行號。
因此,我們在開(kāi)發(fā)代碼階段,保留上述代碼是值得的。
#define new DEBUG_NEW
當進(jìn)行對象轉儲時(shí),用 DEBUG_NEW 分配的每個(gè)對象均將顯示被分配到的文件和行號,使您可以查明內存泄漏源。
MFC 框架的“Debug”版本自動(dòng)使用 DEBUG_NEW,但代碼不自動(dòng)使用它。如果希望利用 DEBUG_NEW 的好處,則必須顯式使用 DEBUG_NEW 或 #define new,如上所示。
情況1:
#ifdef _DEBUG
virtual void AssertValid() const; //assert(斷言)valid(有效的,正確的)
virtual void Dump(CDumpContext& dc) const; //存儲上下文
#endif
這兩個(gè)函數是調試用的,第一個(gè)函數檢查可用性,即是否有效
第二個(gè)函數如果未更改的話(huà),最終調用的是Cwnd::Dump();
輸出窗口類(lèi)名,標題名等一系列信息(在輸出窗口中)
#ifdef _DEBUG
#endif
這是條件編譯,即如果有#define _DEBUG這兩個(gè)函數會(huì )編譯,否則忽略,
當你用debug生成時(shí)(相對于release)開(kāi)發(fā)環(huán)境則自動(dòng)的加上這個(gè)宏定義,這兩個(gè)函數有效。
情況2:
#ifdef _DEBUG // 判斷是否定義_DEBUG
#undef THIS_FILE // 取消THIS_FILE的定義
static char THIS_FILE[]=__FILE__; // 定義THIS_FILE指向文件名
#define new DEBUG_NEW // 定義調試new宏,取代new關(guān)鍵字
#endif // 結束
如果定義了_DEBUG,表示在調試狀態(tài)下編譯,因此相應修改了兩個(gè)符號的定義
THIS_FILE是一個(gè)char數組全局變量,字符串值為當前文件的全路徑,這樣在Debug版本中當程序出錯時(shí)出錯處理代碼可用這個(gè)變量告訴你是哪個(gè)文件中的代碼有問(wèn)題。
定義 _DEBUG后,由于定義了_DEBUG,編譯器確定這是一個(gè)調試,編譯#ifdef _DEBUG和#endif之間的代碼。#undef 表示清除當前定義的宏,使得THIS_FILE無(wú)定義。
__FILE__ 是編譯器能識別的事先定義的ANSI C 的6個(gè)宏之一。#define new DEBUG_NEW
DEBUG_NEW定位內存泄露并且跟蹤文件名.
////////////////////////////////////////////////////////////////////////
情況3:
#ifdef _DEBUG //如果是debug狀態(tài)
#undef THIS_FILE //清除THIS_FILE
static char THIS_FILE[]=__FILE__; //定義THIS_FILE為_(kāi)_FILE__(這是當前文件全路徑名字)
#define new DEBUG_NEW //定義new為DEBUG_NEW(這個(gè)可以檢測到內 //存泄露之類(lèi)的問(wèn)題,其實(shí)就是可以使用crt開(kāi)頭的那幾個(gè)調試函數)
#endif
ANSI C 的6個(gè)宏:6個(gè)分別是
__DATE__
__FILE__
__LINE__
__STDC__
__TIME__
__TIMESTAMP__
編譯C++程序時(shí),編譯器自動(dòng)定義了一個(gè)預處理器名字__cplusplus(代表C++程序),自動(dòng)定義名字__STDC__(代表C程序),__LINE__記錄文件已經(jīng)被編譯的行數__FILE__包含正在被編譯的文件的名字,另外兩個(gè)預定義名字分別包含當前被編譯文件的編譯時(shí)間__TIME__ 和日期__DATE__。
本文來(lái)自CSDN博客,轉載請標明出處:http://blog.csdn.net/nokianasty/archive/2010/11/10/6000512.aspx
聯(lián)系客服