Home > Windows API > DEBUG_NEW explained

DEBUG_NEW explained

mm so what the heck is this DEBUG_NEW? Does this harm my application? Why is DEBUG_NEW provided? These are some of the questions that people have always asked when they see the following statements in a .cpp file of an MFC application…

#ifdef _DEBUG
  #define new DEBUG_NEW
  #undef THIS_FILE
  static char THIS_FILE[] = __FILE__;
#endif

So after all what is this DEBUG_NEW?

Well in short it’s an overloaded version of the new operator. So when you write new in your code the preprocessor expands it to DEBUG_NEW.

It’s proper definition looks likewise…

void* AFX_CDECL operator new(size_t nSize, LPCSTR lpszFileName, int nLine)
{
    return ::operator new(nSize, _NORMAL_BLOCK, lpszFileName, nLine);
}

The call get’s redirected to another overloaded new operator which in turn does some additional housekeeping for tracking memory leaks. This is the reason why this new operator takes the file name and line number so that when a leak occurs the user can be taken to that particular location where the leak occurred. But out of ignorance people just delete above code which although doesn’t disable tracking of leaks but disables the feature whereby the user can double the click in the output window to be taken to that exact location where the leak occurred.

So suppose I write a piece of code which is purposely left undeleted, then this is what I get! So if I click on the red line I am taken to the exact location where the leak occurred.

Detected memory leaks!
Dumping objects ->
E:\SingleDocTest\SingleDocTestView.cpp(43) : {84} normal block at 0x00035628, 100 bytes long.
 Data: <                > CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD 
Object dump complete.

This is a big help to me atleast. I’ve seen colleagues out of ignorance deleting DEBUG_NEW. So how did this help me? Here is an example…

void SomeFrequentlyCalledFunction()
{
    m_pSomeObjPtr = new CSomeObj;
}

So ideally I should be deleting m_pSomeObjPtr before reallocating it again, and this is where DEBUG_NEW comes handy. Well if you have other advanced applications for tracking memory leaks then it’s fine, but this is a big help to know immediately where the leak is happening.

Is there some performance penalty involved?

Yes only in debug builds which is expected too, since we never ship debug builds!

Does this track resource leaks?

Of course not!

Is there anything wrong if we delete it?

Yes it’s wrong but its not an error, leave it, it’s harmless!

Advertisements
  1. November 7, 2008 at 8:48 am

    Well DEBUG_NEW is in .cpp file instead of the header file for me. Isn’t it? But thanks for the link, seems like DEBUG_NEW is always not that helpful.

  2. Kyle Altendorf
    November 6, 2008 at 5:06 pm

    What happens when you include a header for a DLL and DEBUG_NEW goes in and clobbers all of the class member functions called new, free, etc.? For a Microsoft developer’s opinion:

    http://blogs.msdn.com/vcblog/archive/2008/04/07/visual-c-2008-feature-pack-released.aspx#8394544

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: