What is the use of DEBUG_NEW and __FILE__?

I saw the following macros,

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

What is the use of the macro above?

thanks

+6
c ++
source share
5 answers

DEBUG_NEW is just a MACRO, which is usually defined as:

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

So, wherever you use new , it can also keep track of the file and line numbers that you can use to detect memory leaks in your program.

And __FILE__ , __LINE__ are predefined macros that evaluate the file name and line number respectively, where you use them!

Read the following article, which explains how to use DEBUG_NEW with other interesting macros, very nicely:

Cross-platform memory leak detector


From Wikpedia ,

Debug_new refers to a technique in C ++ for overloading and / or overriding a new operator and a delete operator to intercept memory allocation and release calls and thus debug the program to use memory. Often includes a macro definition called DEBUG_NEW and does something like new (_FILE_, _LINE_) to write file / line information to the selection. . Microsoft Visual C ++ uses this method in Microsoft Fundamentals. There are some ways to extend this method to avoid using macro redefinition, the ability to display file / line information about some platforms. There are many inherent to this limitation method. It applies only to C ++ and cannot catch memory leaks using C functions such as malloc. However, it can be very easy to use and also very fast compared to some more complete memory debugging solutions.

+6
source share

_DEBUG is an arbitrarily named but often chosen command line character that indicates that additional code and support should be compiled for debugging the program. Often this causes additional checks to help isolate programming flaws or cause additional messages to be displayed in the interest of the developer.

DEBUG_NEW not clear, but it is probably an alias for new() , which performs an additional check related to new() and delete() .

__FILE__ is an embedded preprocessor character that evaluates the file name of a compiled module. For example, "MyProgram.cc".

+1
source share

One common use of __FILE__ is to design error __FILE__ functions. You can use __FILE__ and __LINE__ together to tell the exact location of the source code where the error occurred.

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

EDIT: Another example here

0
source share

If you are not actually using it, I would advise you to delete this code, because it contradicts other legitimate overloads of the new operator.

See the discussion here:

Is there a way to automatically have #define playable in every source file

0
source share

Typically, this combination of macros and preprocessor instructions is used in MFC projects to track memory leaks. It should be included in your source file, and libral means the following:

Everything between #ifdef _DEBUG and #endif runs only in your compiler's DEBUG mode.

Line: #define new DEBUG_NEW , means that whenever you use the new operator in your code, it will be replaced by the DEBUG_NEW macro. And then when you dump the object, DEBUG_NEW will let you identify the sources of memory leaks.

Lines: #UNDEF THIS_FILE and static char THIS_FILE[] = __FILE__; - override the value of THIS_FILE and remember the current file name and line number.

0
source share

All Articles