What is the point of static char THIS_FILE[] = __FILE__; ?
Introduction: what is he doing? Where is he from?
MFC, Microsoft's native Windows class library, has the DEBUG_NEW macro, which tracks memory allocations and where they were (in user code).
For this to work, the VS wizard puts the following code in each cpp file: (not in the header files)
#ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif
and the new debug macro is defined as (in afx.h ):
#define DEBUG_NEW new(THIS_FILE, __LINE__)
All equipment will lead to a meaningful conclusion of the leak, for example:
Detected memory leaks! Dumping objects -> f:\dd\vctools\vc7libs\ship\atlmfc\src\mfc\strcore.cpp(141) : {615} normal block at 0x04081CE0, 56 bytes long. Data: <¬9Í] > AC 39 CD 5D 13 00 00 00 13 00 00 00 01 00 00 00 c:\my\dev\path\myfile.cpp(237) : {614} normal block at 0x04087FC0, 4 bytes long. Data: <ð > F0 1C 08 04 Object dump complete.
So what's the question again?
What puzzles me is the THIS_FILE char array. Technique does not make sense. If they defined DEBUG_NEW in the same way as:
#define DEBUG_NEW new(__FILE__, __LINE__)
They could just put it in the header and do with it instead of having this ifdef block in every file.
So what is the point of THIS_FILE ?
(By the way, this is exactly what MS CRT does with malloc and _malloc_dbg , where the debug macro is defined in the crtdbg.h header as follows:
#define malloc(s) _malloc_dbg(s, _NORMAL_BLOCK, __FILE__, __LINE__)
)
So why is it a tricky way in the MFC DEBUG_NEW macro when the easy way will work (better) ???
Update: Ha! I recently noticed that the VS2005 wizard does not put the THIS_FILE definition in the generated cpp file.
Examining this, it seems that MS decided some time ago that it was not necessary anymore, since afxtempl.h defines the following:
#undef THIS_FILE #define THIS_FILE __FILE__
However, I think the question remains the same, why it was always necessary. (And I guess the answer with a memory request back then is pretty right.)