#ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif
知道大意,但不喜歡模糊的感受,故對此查閱了一些資料,其中權威MSDN的解釋以下:數組
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.this
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:debug
#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.rest
可能不必翻譯了吧,但因爲週末無聊,仍是小作解釋吧;其實人家已經說得很清楚了,當在debug模式下時,咱們分配內存時的new被替換成DEBUG_NEW,而這個DEBUG_NEW不只要傳入內存塊的大小,還要傳入源文件名和行號,這就有個好處,即當發生內存泄漏時,咱們能夠在調試模式下定位到該問題代碼處。若刪掉該句,就不能進行定位了。而在release版本下的new就是簡單的new,並不會傳入文件名和行號。
若是定義了_DEBUG,表示在調試狀態下編譯,所以相應修改了兩個符號的定義THIS_FILE是一個char數組全局變量,字符串值爲當前文件的全路徑,這樣在Debug版本中當程序出錯時出錯處理代碼可用這個變量告訴你是哪一個文件中的代碼有問題。
code