tags:

views:

993

answers:

1

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?

Thanks
[d3m0n]

+2  A: 

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.

Mark Ransom
Are you sure? VS2008 does still display a memory leak object dump after I've deleted the code block. Maybe this used to be the case in VC6 or something...?
demoncodemonkey
Sorry I just realised there's a subtlety to what you said - when the code is there the output window shows the filename and line containing the memory leak, as opposed to just showing that there is a memory leak.
demoncodemonkey
So that explains the first part of the generated code. What about the 2nd part? #undef THIS_FILE static char THIS_FILE[] = __FILE__;
demoncodemonkey
I believe THIS_FILE is what DEBUG_NEW uses to get the source filename that it uses for the leak report.
Mark Ransom
Yeah that does sound right, but it's a bit weird that the THIS_FILE code is only present in some source files and not others. Thanks for helping me figure it out :)
demoncodemonkey