我正在尝试替换全局运算符 new 和 delete。在 Linux 中,这工作正常,但在 Windows(MSVC 10)中,它有时使用 operator new 的系统版本进行分配,然后尝试使用我的 operator delete 解除分配。因为我在分配时存储了一些上下文信息,所以我的运算符(operator) delete 在解除分配期间期望相同。我如何确保 Windows 始终接收我的新运算符(operator)?
编辑: 我尝试过各种东西。这些是声明
//Global new and delete overload
void* operator new (std::size_t bytes) throw(...);
void operator delete(void* p) throw();
void* operator new( std::size_t size, const std::nothrow_t&) throw();
void operator delete( void* mem, const std::nothrow_t&) throw();
void* operator new[] ( std::size_t bytes) throw(...);
void operator delete[](void* p) throw();
void* operator new[]( std::size_t size, const std::nothrow_t&) throw();
void operator delete[](void* mem, const std::nothrow_t&) throw();
#ifdef WIN32
void *__CRTDECL operator new(std::size_t size) _THROW1(_STD bad_alloc);
#endif
这些是定义
#ifdef WIN32
void *__CRTDECL operator new(std::size_t bytes) _THROW1(_STD bad_alloc)
{
void* p = edb::g_getCurrentMemoryContext()->alloc(bytes);
if (p==0) // did malloc succeed?
throw std::bad_alloc(); // ANSI/ISO compliant behavior
return p;
}
#endif
void operator delete(void* p) throw()
{
edb::MemContext::free(p);
}
void* operator new( std::size_t bytes, const std::nothrow_t&) throw()
{
return edb::g_getCurrentMemoryContext()->alloc(bytes);
}
void operator delete(void* p, const std::nothrow_t&) throw()
{
edb::MemContext::free(p);
}
void* operator new[] ( std::size_t bytes) throw(...)
{
void* p = edb::g_getCurrentMemoryContext()->alloc(bytes);
if (p==0) // did malloc succeed?
throw std::bad_alloc(); // ANSI/ISO compliant behavior
return p;
}
void operator delete[](void* p) throw()
{
edb::MemContext::free(p);
}
void* operator new[]( std::size_t bytes, const std::nothrow_t&) throw()
{
return edb::g_getCurrentMemoryContext()->alloc(bytes);
}
void operator delete[](void* p, const std::nothrow_t&) throw()
{
edb::MemContext::free(p);
}
有时它会接受我的定义,有时不会。
谢谢, 悟空。
最佳答案
在 C++ 中覆盖全局新建/删除是一个 tar baby .是的,它看起来很简单,但是你一个接一个地破例,你就不断地为自己挖一个越来越深的坑。
你得到“他们的新”和“你的删除”的原因是你可能正在使用一些已经加载的 DLL 并在加载你的之前“链接”到现有的新的。他们分配对象,然后您应该删除它。您可以尝试静态链接您的程序来解决这个问题。
另一种处理方法是重构您的代码并将 new/delete 覆盖为基类,然后让您的所有类都继承自该基类。对于一个有很多类的大型项目来说很乏味,但它会消除为谁调用哪个 new/delete 的歧义。
如果您这样做可能是一种隔离内存泄漏的“廉价”方法,请考虑其他方法。查看是否有任何仪器工具供应商(例如 purify)仍有“2 周”试用期或通过 valgrind 运行您的 Linux 版本.
关于c++ - Windows 中的运算符新重载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6058509/