删除通过放置分配的对象 新建?

Deleting an Object Allocated Through Placement New?

提问人:HCSF 提问时间:3/27/2023 最后编辑:HCSF 更新时间:3/27/2023 访问量:86

问:

我有以下代码:

struct SomeStruct2 {
    // has 4 primitive types of variables
};

struct SomeStruct1 {
    // has 10 primitive types of variables
    std::deque<SomeStruct2> foo;
};

template <class Data>
class ResourcePool {
public:
    void recycle(Data* data_ptr) {
        // do some work
        data_ptr->~data_ptr();
    }
};

由于某些原因,每隔几天(每天运行应用程序大约 8 小时后),它就会进行核心转储:

(gdb) bt

#0  0x00007fa443e32387 in raise () from /lib64/libc.so.6
#1  0x00007fa443e33a78 in abort () from /lib64/libc.so.6
#2  0x00007fa443e74f67 in __libc_message () from /lib64/libc.so.6
#3  0x00007fa443e7d329 in _int_free () from /lib64/libc.so.6
#4  0x0000000000434528 in __gnu_cxx::new_allocator<MyClass::SomeStruct2>::deallocate (this=0x87a410, __p=0x86f260, __t=16) at /opt/rh/devtoolset-10/root/usr/include/c++/10/ext/new_allocator.h:133
#5  0x000000000043147e in deallocate (__n=16, __p=0x86f260, this=0x87a410) at /opt/rh/devtoolset-10/root/usr/include/c++/10/bits/allocator.h:187
#6  std::allocator_traits<std::allocator<MyClass::SomeStruct2> >::deallocate (__a=..., __p=0x86f260, __n=16) at /opt/rh/devtoolset-10/root/usr/include/c++/10/bits/alloc_traits.h:492
#7  0x000000000042e73a in std::_Deque_base<MyClass::SomeStruct2, std::allocator<MyClass::SomeStruct2> >::_M_deallocate_node (this=0x87a410, __p=0x86f260) at /opt/rh/devtoolset-10/root/usr/include/c++/10/bits/stl_deque.h:566
#8  0x000000000042b8ae in std::_Deque_base<MyClass::SomeStruct2, std::allocator<MyClass::SomeStruct2> >::_M_destroy_nodes (this=0x87a410, __nstart=0x86f988, __nfinish=0x86f990) at /opt/rh/devtoolset-10/root/usr/include/c++/10/bits/stl_deque.h:676
#9  0x00000000004263ad in std::_Deque_base<MyClass::SomeStruct2, std::allocator<MyClass::SomeStruct2> >::~_Deque_base (this=0x87a410, __in_chrg=<optimized out>) at /opt/rh/devtoolset-10/root/usr/include/c++/10/bits/stl_deque.h:598
#10 0x000000000042643f in std::deque<MyClass::SomeStruct2, std::allocator<MyClass::SomeStruct2> >::~deque (this=0x87a410, __in_chrg=<optimized out>) at /opt/rh/devtoolset-10/root/usr/include/c++/10/bits/stl_deque.h:1004
#11 0x00000000004270c2 in MyClass::SomeStruct1::~SomeStruct1 (this=0x87a388, __in_chrg=<optimized out>) at my.hpp:67
#12 0x0000000000427109 in ResourcePool<MyClass::SomeStruct1>::recycle (this=0x7ffc3a6f9480, pData=0x87a388) at ResourcePool.hpp:94
#13 0x0000000000422579 in MyClass::try_recycle (this=0x7ffc3a6f9270, data_ptr=0x87a388) at my.hpp:292
#14 0x0000000000421cf9 in MyClass::timer_expired_callback (local_timer_id=8845808, epochExpiration=1679894068466539651, opaqueData=0x7ffc3a6f9270) at my.hpp:453

然后我运行 gdb 转到第 11 帧,内容看起来没有损坏。p *this

知道为什么当我尝试删除通过放置新分配的对象时,它有时会导致核心转储吗?奇怪的是,当它试图释放内部时,它就死了。 显示没有参数。std::dequeSomeStruct1gdbstd::deque

C++ C++17 放置-新

评论

1赞 463035818_is_not_an_ai 3/27/2023
请发布一个最小的可重现示例。核心转储肯定不是来自您在此处包含的代码
0赞 molbdnilo 3/27/2023
事物不必仅仅因为它们不存在而看起来已损坏。
1赞 user253751 3/27/2023
此位置的崩溃可能意味着您的程序损坏了堆,这是其他一些不相关的堆操作检测到堆已损坏的地方。请注意,该功能__libc_message提示已打印消息(并且您未看到)。运行程序通常会很快发现内存损坏 - 请注意,程序运行速度较慢,因为它是模拟的。valgrind
0赞 HCSF 3/27/2023
@463035818_is_not_a_number是的,你是对的。我希望我能。但它有一些 IP 问题,所以我无法发布最小的可重复示例。
1赞 Richard Critten 3/27/2023
您可能需要运行“地址清理程序”版本,但这会对服务器性能产生相当大的影响,因此可能仅在测试环境中。

答: 暂无答案