gpt4 book ai didi

c++ - 第三方库不调用全局重载delete[]

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:37:41 31 4
gpt4 key购买 nike

我正在尝试了解 C++ 中的内存池以获得更好的速度和调试能力。我一直在遵循此处找到的方法:http://oroboro.com/overloading-operator-new/ .所以我重载了 newnew[]deletedelete[],如下所示:

inline void* operator new     ( size_t size ) { return myAlloc( size ); }
inline void* operator new[] ( size_t size ) { return myAlloc( size ); }
inline void operator delete ( void* ptr ) { myFree( ptr ); }
inline void operator delete[]( void* ptr ) { myFree( ptr ); }

我喜欢将第三方库定向到此版本的 new,但我遇到了一个问题。我正在制作 DirectX使用 DXUT 的应用程序。我从我的项目中单独编译 DXUT。最终它调用:

std::unique_ptr<D3D11_SUBRESOURCE_DATA[]> initData( new (std::nothrow) D3D11_SUBRESOURCE_DATA[ mipCount * arraySize ] );

一旦这个唯一指针超出范围,它就会在调用 delete[] _Ptr 时崩溃,这没有通过我的重载运算符。我尝试通过添加 int* dummy = new int[10]; 来调试我的内存池实现; delete[] dummy; 在我的 main 中。构建项目出错,但 clean-building 工作正常。令我惊讶的是,一切正常,包括崩溃的 DXUT 线!

问题 1:当我添加修复问题的调试行时到底发生了什么?我想出于某种原因我的操作符 delete[] 直到我在自己的应用程序代码中调用它才为人所知?这是否可以保证解决问题,还是只是运气不好?

问题 2:我注意到 new (std::nothrow) D3D11_SUBRESOURCE_DATA[ mipCount * arraySize ] 没有直接调用我的 operator new[],但最终调用了我的 operator new(无括号)。它仍然在指针上调用运算符 delete[]。这会造成问题吗?我是否必须添加适当的重载以便调用我的 operator new[] 或者这种行为是否正常?

作为引用,调用的 operator new[] 重载是:

void * __CRTDECL operator new[](::size_t count, const std::nothrow_t& x)
_THROW0()
{ // Try to allocate count bytes for an array
return (operator new(count, x));
}

最佳答案

Question 1: What exactly happened when I added the debugging line that fixed the issue? I guess for some reason my operator delete[] was not known until I called it in my own application code? Is this guaranteed to fix the problem or is it just dumb luck?

§3.2 [basic.def.odr]/p4:

An inline function shall be defined in every translation unit in which it is odr-used.

听起来你的编译器没有为全局 operator delete[] 生成代码,因为它没有在你的主翻译单元中使用并且被标记为 inline(意思是编译器可以假设任何使用它们的翻译单元都会有它们的定义)。但是,您单独编译的库没有这些函数的定义,您最终会使用标准库中的默认函数。使它们成为非内联应该可以解决问题。

Question 2: I noticed that the new (std::nothrow) D3D11_SUBRESOURCE_DATA[ mipCount * arraySize ] did not call my operator new[] directly, but eventually called my operator new (no brackets). It still calls operator delete[] on the pointer. Does this pose an issue? Do I have to add the appropriate overload such that my operator new[] is called or is this behavior fine?

operator new [] 的抛出和非抛出版本的默认版本,以及 operator new 的非抛出版本,被指定为调用抛出版本operator new 获取内存。所以你在那里很安全。但是,您的 operator new 的定义可能是错误的。它们必须返回一个有效的指针或抛出异常。不允许返回空指针。

关于c++ - 第三方库不调用全局重载delete[],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25596656/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com