VC里的#define new DEBUG_NEW
以下代碼常常在一個類文件的開頭出現(xiàn),是什么意思呢?
#ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif先看MSDN里的解釋:
Remarks
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.
?
? ? 這樣就很清楚了,當在debug模式下時,我們分配內存時的new被替換成DEBUG_NEW,而這個DEBUG_NEW不僅要傳入內存塊的大小,還要傳入源文件名和行號,這就有個好處,即當發(fā)生內存泄漏時,我們可以在調試模式下定位到該問題代碼處。若刪掉該句,就不能進行定位了。而在release版本下的new就是簡單的new,并不會傳入文件名和行號。
? ? 如果定義了_DEBUG,表示在調試狀態(tài)下編譯,因此相應修改了兩個符號的定義THIS_FILE是一個char數(shù)組全局變量,字符串值為當前文件的全路徑,這樣在Debug版本中當程序出錯時出錯處理代碼可用這個變量告訴你是哪個文件中的代碼有問題。
轉載于:https://www.cnblogs.com/imapla/archive/2012/09/03/2668859.html
總結
以上是生活随笔為你收集整理的VC里的#define new DEBUG_NEW的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: POJ1724 ROADS 费用最短路
- 下一篇: 1595 hdu find the lo