重载新操作符问题
我决定在我的类中重载 new、new[]、... 运算符,以便我可以记录调用它们的文件和行,以便我可以更轻松地跟踪内存分配/泄漏。
现在问题出在我的堆栈和数组类(以及分配内存的其他模板容器类)中:
如果我将它们与重载了 new,new[],... 运算符的类之一一起使用,则它可以正常工作。
但如果我将它与标准 C++ 数据类型(int、float、...)一起使用,我将无法分配它们,因为没有重载的 new 运算符与 new(__ LINE __ , __ FILE __) 运算符的参数匹配(或其他人喜欢新的安置)。
堆栈代码示例:
// placement new
T* t=new(__ LINE __ , __ FILE__)(&m_data[i])T;
所以我对如何完成这项工作没有好主意。如果我用 new 替换 new(__ LINE __ ,__ FILE __) ,我就会失去内存记录能力。 一种解决方案是为标准数据类型创建一个单独的堆栈,其中使用默认的 new。
有什么方法可以在编译时检测模板参数是否是结构、类或内置 C++ 类型?
你如何处理这样的事情? 你有什么建议? 对这个设计的任何评论(好,坏)显然都是受欢迎的(只是不要发布诸如“不要用自己的容器重新发明轮子”之类的东西)。
I decided to overload the new, new[],... operators in my classes so I can log the file and line at which they were called so I can easier track memory allocations/leaks.
Now the problems is in my stack and array classes (and other template container classes which allocate memory):
If I use them with one of my classes which has the new,new[],... operators overloaded it works fine.
But if I use it with the standard c++ data types (int,float,...) I can't allocate them, since no overloaded new operator matches the arguments of the new(__ LINE __ , __ FILE __) operator (or others like placement new).
Example of stack code:
// placement new
T* t=new(__ LINE __ , __ FILE__)(&m_data[i])T;
So I'm out of good ideas on how to make this work. If I replace new(__ LINE __ ,__ FILE __) with new I loose memory logging ability.
One solution is to make a separated stack for standard data types in which the default new is used.
Is there any way to detect at compile time if a template parameter is a struct, class or a built in c++ type?
How do you handle stuff like this?
What do you suggest?
Any comments on this design (good,bad) are obviously welcome (just don't post stuff like "don't reinvent the wheel with your own containers ").
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
请注意,您当前的解决方案需要将日志记录代码添加到您拥有的每个
new(line, file)
重载中。此外,您无法在发布版本中轻松将其关闭,除非您将每个日志记录调用包含在#ifndef DEBUG ... #endif
中。以下是实现您想要的目标的一种方法:不要为每个类重载
new
运算符,而是考虑使用放置语法重载全局new
运算符。 EM>;这样您就可以避免干扰“正常”new
运算符。然后,您可以#define
new 和delete 宏以方便起见,最重要的是,您可以控制何时应用内存跟踪new/delete
以及何时应用标准版本被使用。您应该看到类似的内容:
尝试用
X
替换int
,您会发现它也有效。您也可以将其扩展到新的数组和位置,但我不想让帖子比现在更长。最后几点:
- MSVC 具有此功能,请参阅此处
- 此处在“跟踪内存”下有一个关于以这种方式进行内存跟踪的教程泄密部分
Please note that your current solution requires adding the logging code to every
new(line, file)
overload you have. Also, you cannot easily switch it off in release builds unless you surround each of your logging calls within#ifndef DEBUG ... #endif
.Here's one way of achieving what you wnat: Instead of overloading the
new
operator for each of your classes, consider overloading the globalnew
operator using the placement syntax; that way you avoid interfering with the 'normal'new
operator. Then you can#define
new and delete macros for convenience and, most importantly, you can have control over when your memory-trackingnew/delete
is applied and when the standard version is used.you should see something like:
Try substituting
X
forint
and you'll see it works too. You can extend this to the array and placement new as well but i'd rather not make the post longer than it is.Few last pointers at the end:
- MSVC has this functionality, see here
- There is a toturial about doing memory tracking this way here under the 'Tracing Memory Leaks' section
使用此宏而不是标准行号宏,重载解析会将
Int
行号与int
其他数字区分开来。我无法想象这将如何完全解决。您打算像
int * x = NEW(int,123);
或类似的东西使用它吗?顺便说一句,我同意评论者的观点——你可能不必走这条路。重载
new
是一种魔法,通常应该避免。Use this macro instead of the standard line number macro and overload resolution will distinguish
Int
line numbers fromint
other numbers.I can't imagine how this will work out in full. Are you going to use it like
int * x = NEW(int,123);
or something like that?I agree with the commenters, by the way -- you probably don't have to go down this road. Overloading
new
is something of a black art and should normally be avoided.您可以使用 boos::type_traits 和 boost::mpl 来实现。
示例:
类型列表 -
http://www.boost.org/doc /libs/1_47_0/libs/type_traits/doc/html/index.html
或者您可以使用 boost::mpl::set 作为您的类型集
You can use boos::type_traits and boost::mpl for it.
Example:
Types list -
http://www.boost.org/doc/libs/1_47_0/libs/type_traits/doc/html/index.html
Or you can use boost::mpl::set for your set of types