Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are "#define new DEBUG_NEW" and "#undef THIS_FILE" etc. actually necessary?

When you create a new MFC application, the wizard creates the following block of code in almost every CPP file:

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

and sometimes it also adds this:

#undef THIS_FILE
static char THIS_FILE[] = __FILE__;

I would like to remove this code from my CPP files if it is redundant. I am using an MFC app with C++/CLI on VS2008.

I have tried running in Debug after deleting this code from a CPP, and it seems to work fine. "new"ing variables work fine, there are no leaks, and ASSERT dialogs show the correct filename and jump to the offending line.

Can anyone tell me what it does and whether it's safe to delete it?

like image 517
demoncodemonkey Avatar asked Feb 06 '09 16:02

demoncodemonkey


2 Answers

It is perfectly safe to delete this. It's a debugging aid; leaving it in will generate better details in the warnings in the output window of any memory leaks you have when the program exits. If you delete it, you still get the memory leak report, but just without any details about where in your source code they occurred.

like image 67
Mark Ransom Avatar answered Nov 17 '22 18:11

Mark Ransom


On Microsoft Visual C++ 2010, I can remove the whole code and put just one #define NEW DEBUG_NEW in a header, and I still got the right memory leak reports, e.g.

Detected memory leaks!
Dumping objects ->
f:\dd\vctools\vc7libs\ship\atlmfc\src\mfc\strcore.cpp(156) : {7508} normal block at 0x029B9598, 54 bytes long.
 Data: <                > E4 B8 C9 00 12 00 00 00 12 00 00 00 01 00 00 00 
f:\dd\vctools\vc7libs\ship\atlmfc\src\mfc\strcore.cpp(156) : {7501} normal block at 0x029B94A8, 28 bytes long.
 Data: <                > E4 B8 C9 00 05 00 00 00 05 00 00 00 01 00 00 00 
f:\source\agent\agent\deviceid.cpp(21) : {7500} normal block at 0x029CDFC0, 8 bytes long.
 Data: <        > A8 95 9B 02 B8 94 9B 02 
f:\dd\vctools\vc7libs\ship\atlmfc\src\mfc\strcore.cpp(156) : {6786} normal block at 0x029C0D88, 160 bytes long.
 Data: <        G       > E4 B8 C9 00 19 00 00 00 47 00 00 00 01 00 00 00 
f:\source\agent\sysinfo\sysinfo.cpp(27) : {6733} normal block at 0x029B84D8, 92 bytes long.
 Data: <                > 00 00 00 00 00 10 00 00 00 00 01 00 FF FF FE 7F 
Object dump complete.
like image 35
Samphan Avatar answered Nov 17 '22 18:11

Samphan