I have the following code:
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();
}
};
For some reasons, every few days (after running the application for like 8 hours a day), it would coredump:
(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
I then ran gdb to go to frame 11 and p *this, the content doesn't look like corrupted.
Any idea why it caused coredump sometimes when I tried to delete an objected allocated through placement new? The strange thing is that it died when it tried to deallocate the std::deque inside SomeStruct1. gdb shows that the std::deque has no argument.