【发布时间】:2017-03-06 11:51:36
【问题描述】:
当我全局重载 new 运算符以跟踪内存泄漏时,在以下位置出现编译错误
::new(__tmp) _Rb_tree_node<_Val>;
看起来他们在那里分配和填充节点。 以下是错误:
/home/symbol/Android/Sdk/ndk-bundle/sources/cxx-stl/gnu-libstdc++/4.9/include/functional: In static member function 'static void std::_Function_base::_Base_manager<_Functor>::_M_clone(std::_Any_data&, const std::_Any_data&, std::true_type)':/home/symbol/Android/Sdk/ndk-bundle/sources/cxx-stl/gnu-libstdc++/4.9/include/functional:1870:9: error: '__dest' does not name a type
new (__dest._M_access()) _Functor(__source._M_access<_Functor>());
下面是我的重载代码:
void* operator new(std::size_t sz,char const* file, int line){
return newImpl(sz,file,line);
}
void* operator new [](std::size_t sz,char const* file, int line){
return newImpl(sz,file,line);
}
void operator delete(void* ptr) noexcept{
auto ind=std::distance(myAlloc.begin(),std::find(myAlloc.begin(),myAlloc.end(),ptr) );
myAlloc[ind]= nullptr;
free(ptr);
}
#define new new(__FILE__, __LINE__)
【问题讨论】:
-
宏定义看起来不对。生成预处理器输出以查看该宏在代码中扩展为什么,这可能不是您所期望的。
-
如果您仍然使用宏,为什么还需要重载?你可以用任何函数调用替换
new
标签: c++ c++11 operator-overloading new-operator