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?

Answers


It is perfectly safe to delete this. It's a debugging aid; leaving it in will generate warnings in the output window of any memory leaks you have when the program exits.


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.

Need Your Help

create classes at runtime from database

c# asp.net database

How can I create classes by using database entries in runtime?

merge multiple tr with same value/ID in jquery

jquery table toggle tr

Its been so long since i researched and found nothing. This was the problem im facing, i have a table, populated from database. All i want to do is to merge all the table rows with same value(pati...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.