覆盖新的调试版本,而不损害位置新 [英] Overriding new with debug version without damaging placement new

查看:159
本文介绍了覆盖新的调试版本,而不损害位置新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Microsoft运行时库提供调试版本的分配函数。对于C ++,这是一个具有签名的操作符new的调试变体:

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);

,宏定义为

#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

然而,这个定义打破了使用placement new的任何地方,因为两组参数最终是语法错误。现在我可以轻松地处理我们的代码中的少数用途,但标准库和boost使用放置新的所有地方。因此,定义这个全局意味着在定义之前包含很多东西,并减慢编译速度。

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.

因此,有没有办法在我们的代码中分配,只是因为它们包含新的布局,而不必在所有文件中放上最后一个定义或手动编写DEBUG_NEW。

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?

推荐答案

我已经解决了这个历史上是通过使用预编译头,并做这样的事情(StdAfx.h,Pch.h,PreCompiled.h或任何):

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__)   


$ b b

然后确保没有文件直接包含boost标头,而只包含预编译头。

And then make sure no files include the boost headers directly but only the precompiled header.

这篇关于覆盖新的调试版本,而不损害位置新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆