Overriding the new version of debugging without prejudice to posting new

The Microsoft Runtime Library provides a debug version of the distribution functions. For C ++, this is a debugging option for the new operator with a signature:

void *operator new(size_t size, int blockType, const char *filename, int linenumber); 

and the macro is defined as

 #define DEBUG_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__) 

Now, to measure all distributions, it is usually determined

 #if defined DEBUG_NEW #define new DEBUG_NEW #endif 

However, this definition breaks down any place that uses the new location because two sets of arguments become a syntax error. Now I can easily handle several uses in our code, but the standard library and speeding up the use of placement is ubiquitous. Thus, defining this globally means including a lot of material before defining and slows down compilation.

So, is there any way to distribute the tools in our code without pulling out the headers just because they contain a new location and should not either put the last definition higher in all files or manually write DEBUG_NEW?

+6
source share
5 answers

This whole thing DEBUG_NEW must die on fire! It was only ever a little useful, and it is not useful in modern C ++ at all, because you no longer see new in robust C ++.

Better options have appeared, such as DUMA, and now that there is Dr. Memory (which works similarly to Valgrind , but on Windows) absolutely does not make sense to use the abomination DEBUG_NEW.

0
source
 #pragma push_macro("new") #undef new new(pointer) my_class_t(arg1, arg2); #pragma pop_macro("new") 

or

 #pragma push_macro("new") #undef new #include <...> #include <...> #include <...> #pragma pop_macro("new") 
+5
source

The way I solved this historically is to use precompiled headers and do something like this (StdAfx.h, Pch.h, PreCompiled.h or something else):

 //First include all headers using placement new #include <boost/tuple/tuple.hpp> #include <vector> #define new MY_NEW #define MY_NEW new(__FILE__, __LINE__) 

And then make sure the files don't contain boost headers directly, but only a precompiled header.

+3
source

I know this is pretty late, but this problem can be solved using the magic of templates.

Recently, I have coded the debug_new debugger, which adds a new keyword "placement", which receives written information about all new placement calls.

You can check my debugger here: https://sourceforge.net/projects/debugnew/

Or debug_new debugger from nvwa here: https://sourceforge.net/projects/nvwa/

0
source

define new DEBUG_NEW line should be placed in the source files, after all #include lines. Thus, it applies only to your own code and does not apply to other h files such as Boost. The global new to override DEBUG_NEW may cause compilation to fail and should be avoided.

-1
source

Source: https://habr.com/ru/post/927325/


All Articles