greetings,
my test code is as follows:
"
int k = 128;
std::vector v(k);
for(unsigned int i = 0; i < k; ++i)
{
v[i].reset(&k);
// v[i].reset(new int(0));
}
int t = rand() % k + 1;
std::cout << "element " << t << " points to integer value " << *(v[t].get()) << std::endl;
}
"
It's intended to use shared_ptr in an atypical way(the normal one was commented out). The output is:
"
element 68 points to integer value 128
Segmentation fault
"
Attached debugger and got this stack:
"
Program received signal SIGSEGV, Segmentation fault.
0x0000002a95c16ed0 in _int_free () from /lib64/tls/libc.so.6
(gdb) bt
#0 0x0000002a95c16ed0 in _int_free () from /lib64/tls/libc.so.6
#1 0x0000002a95c17317 in free () from /lib64/tls/libc.so.6
#2 0x0000002a95862cee in operator delete () from /usr/lib64/libstdc++.so.5
#3 0x0000000000403ac7 in boost::checked_delete<int> (x=0x7fbfffe9c8) at checked_delete.hpp:34
#4 0x00000000004035b4 in boost::detail::sp_counted_impl_p<int>::dispose (this=0x509060) at sp_counted_impl.hpp:78
#5 0x0000000000402cf8 in boost::detail::sp_counted_base::release (this=0x509060) at sp_counted_base_gcc_x86.hpp:146
#6 0x0000000000402dee in _ZN5boost6detail12shared_countD9Ev (this=0x508058) at shared_count.hpp:199
#7 0x0000000000402e0d in boost::detail::shared_count::~shared_count (this=0x508058) at shared_count.hpp:65536
#8 0x0000000000402831 in destr_detour25 () at shared_ptr.hpp:65536
#9 0x0000000000402855 in boost::shared_ptr<int>::~shared_ptr (this=0x508050) at sp_counted_base_gcc_x86.hpp:110
#10 0x0000000000403822 in std::_Destroy (__pointer=0x508050) at stl_construct.h:125
#11 0x00000000004037f6 in std::__destroy_aux (__first=0x508050, __last=0x509050) at stl_construct.h:101
#12 0x00000000004037c7 in std::_Destroy (__first=0x508050, __last=0x509050) at stl_construct.h:143
#13 0x0000000000401d3f in _ZNSt6vectorIN5boost10shared_ptrIiEESaIS2_EED9Ev (this=0x7fbfffe900) at stl_vector.h:297
#14 0x0000000000401da1 in std::vector >::~vector (this=0x7fbfffe900) at stl_vector.h:65536
#15 0x000000000040160a in destr_detour44 () at shared_ptr.cpp:42
#16 0x0000002a95bcaacd in __libc_start_main () from /lib64/tls/libc.so.6
#17 0x000000000040108a in _start () at start.S:96
"
I expected some problems with the code, but the segfault surprised me. Can anybody tell me why?
Thanks a lot,