Overriding new with debug version without damaging placement new

对着背影说爱祢 提交于 2019-12-21 12:36:09

问题


Microsoft runtime library provides debug version of allocation functions. For C++ this is a debug variant of operator new with the signature:

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

and a macro is defined like

#define DEBUG_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__)

Now to instrument all allocations, one normally defines

#if defined DEBUG_NEW
#define new DEBUG_NEW
#endif

However this definition breaks any place that uses placement new, because the two sets of arguments end up being syntax error. Now I can easily handle the few uses in our code, but the standard library and boost use placement new all over the place. So defining this globally means including a lot of stuff before the definition and that slows down compilation.

So would there be any way to instrument allocations in our code without pulling in headers just because they contain placement new and without having to either put the last define above in all files or writing DEBUG_NEW manually?


回答1:


#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")



回答2:


The way I've solved this historically is by using precompiled headers, and doing something like this (StdAfx.h, Pch.h, PreCompiled.h or whatever):

//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 no files include the boost headers directly but only the precompiled header.




回答3:


I know this is kind of late, but that problem can be solved by using template magic.

I've been lately coding a debug_new debugger, that adds a new keyword "placement", that gets writen infront of all placement new calls.

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

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




回答4:


This whole DEBUG_NEW thing should die in fire! It was only ever barely useful and it is not useful in modern C++ at all, because you no longer see new in sane C++.

There were better options like DUMA and now that there is Dr. Memory (which works similarly to Valgrind, but on Windows), there is absolutely no point in using the DEBUG_NEW abomination.




回答5:


define new DEBUG_NEW line should be placed in a source files, after all #include lines. By this way it is applied only to your own code, and not applied to any other h-files like Boost. Global new to DEBUG_NEW redifinition may cause compilation to fail and should be avoided.



来源:https://stackoverflow.com/questions/12815261/overriding-new-with-debug-version-without-damaging-placement-new

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!