Overriding new with debug version without damaging placement new

筅森魡賤 提交于 2019-12-04 05:35:25
#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")

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.

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/

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.

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.

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