提问人:HCSF 提问时间:3/27/2023 最后编辑:HCSF 更新时间:3/27/2023 访问量:86
删除通过放置分配的对象 新建?
Deleting an Object Allocated Through Placement New?
问:
我有以下代码:
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::deque
SomeStruct1
gdb
std::deque
答: 暂无答案
评论
valgrind