8

I saw the follow macros,

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

What is the usage of above macro?

Thank you

q0987
  • 34,938
  • 69
  • 242
  • 387
  • Is this still relevant when compiling with e.g. VS2017? I've got this in my old code and wondering if it can be removed. – Viktor Be May 19 '20 at 08:34

5 Answers5

13

DEBUG_NEW is just a MACRO which is usually defined as:

#define DEBUG_NEW new(__FILE__, __LINE__)
#define new DEBUG_NEW

So that wherever you use new, it also can keep track of the file and line number which could be used to locate memory leak in your program.

And __FILE__, __LINE__ are predefined macros which evaluate to the filename and line number respectively where you use them!

Read the following article which explains the technique of using DEBUG_NEW with other interesting macros, very beautifully:

A Cross-Platform Memory Leak Detector


From Wikpedia,

Debug_new refers to a technique in C++ to overload and/or redefine operator new and operator delete in order to intercept the memory allocation and deallocation calls, and thus debug a program for memory usage. It often involves defining a macro named DEBUG_NEW, and makes new become something like new(_FILE_, _LINE_) to record the file/line information on allocation. Microsoft Visual C++ uses this technique in its Microsoft Foundation Classes. There are some ways to extend this method to avoid using macro redefinition while still able to display the file/line information on some platforms. There are many inherent limitations to this method. It applies only to C++, and cannot catch memory leaks by C functions like malloc. However, it can be very simple to use and also very fast, when compared to some more complete memory debugger solutions.

Nawaz
  • 353,942
  • 115
  • 666
  • 851
1

_DEBUG is an arbitrarily named, but often chosen, command line symbol which indicates that extra code and support for debugging the program should be compiled in. Often this causes extra checks to help isolate programming flaws, or causes extra messages to be output for the benefit of the developer.

DEBUG_NEW isn't clear, but it is probably an alias for new() which does extra validation associated with new() and delete().

__FILE__ is a built-in preprocessor symbol which evaluates to the filename of the module being compiled. For example "MyProgram.cc".

wallyk
  • 56,922
  • 16
  • 83
  • 148
0

Usually, this combination of makroses and preprocessor commands, is used in MFC projets, to track memory-leaks. It has to be included into you source file, and literraly means following:

Everything between #ifdef _DEBUG and #endif executed only in DEBUG mode of your compiler.

The string: #define new DEBUG_NEW, means that whenever you use operater new in your code, it will be replaced by macro DEBUG_NEW. And then, when you do an object dump, DEBUG_NEW will allow you to pinpoint the sources of memory leaks.

The strings: #UNDEF THIS_FILE and static char THIS_FILE[] = __FILE__; - redefine the meaning of THIS_FILE and remembers the current file-name and line number.

Slav
  • 35
  • 1
  • 9
0

One common usage of __FILE__ is in designing error-logging functions. You can use __FILE__ and __LINE__ together to report the exact source code location where the error occurred.

I have designed exception libraries and assert()-type functions that log this information. One such use is documented here.

EDIT: Another example here

Community
  • 1
  • 1
John Dibling
  • 99,718
  • 31
  • 186
  • 324
0

If you don't actually use it, I would advice you to remove that code, because it comflicts with other ligitimate overloads of operator new.

See the discussion here:

Is there a way to automatically have a #define reproduced in each source file

Community
  • 1
  • 1
Bo Persson
  • 90,663
  • 31
  • 146
  • 203