[shared_ptr] why segfault?
greetings, my test code is as follows: " int k = 128; std::vector<boost::shared_ptr<int> > 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<boost::shared_ptr<int> > (__pointer=0x508050) at stl_construct.h:125 #11 0x00000000004037f6 in std::__destroy_aux<boost::shared_ptr<int>*> (__first=0x508050, __last=0x509050) at stl_construct.h:101 #12 0x00000000004037c7 in std::_Destroy<boost::shared_ptr<int>*> (__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<boost::shared_ptr<int>, std::allocator<boost::shared_ptr<int> > >::~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,
Xu, Peng wrote:
greetings,
my test code is as follows: " int k = 128; std::vector<boost::shared_ptr<int> > v(k); for(unsigned int i = 0; i < k; ++i) { v[i].reset(&k); // v[i].reset(new int(0)); }
v[i].reset(&k) assumes that &k was allocated via a call to new. Since it wasn't, you will eventually get a segfault when the shared pointer at v[i] attempts to delete &k. To get around this, use the version of reset() that takes a pointer and a deleter object. struct null_deleter { void operator()(const void *) {} // Do-nothing operator() }; int k = 128; std::vector<boost::shared_ptr<int> > v(k); for (unsigned int i = 0; i < k; ++i) { v[i].reset(&k, null_deleter()); } Joe Gottman
-----Original Message----- From: boost-users-bounces@lists.boost.org [mailto:boost-users-bounces@lists.boost.org] On Behalf Of Joe Gottman Sent: Tuesday, March 20, 2007 6:10 PM To: boost-users@lists.boost.org Subject: Re: [Boost-users] [shared_ptr] why segfault? Xu, Peng wrote:
greetings,
my test code is as follows: " int k = 128; std::vector<boost::shared_ptr<int> > v(k); for(unsigned int i = 0; i < k; ++i) { v[i].reset(&k); // v[i].reset(new int(0)); }
v[i].reset(&k) assumes that &k was allocated via a call to new. Since it wasn't, you will eventually get a segfault when the shared pointer at v[i] attempts to delete &k. To get around this, use the version of reset() that takes a pointer and a deleter object. struct null_deleter { void operator()(const void *) {} // Do-nothing operator() }; int k = 128; std::vector<boost::shared_ptr<int> > v(k); for (unsigned int i = 0; i < k; ++i) { v[i].reset(&k, null_deleter()); } Joe Gottman That helps. Thanks a lot! _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
I checked out boost HEAD recently and tried to use asio library. In folder PATH/boost/libs/asio, I did a "firefox index.html". It cannot find PATH/boost/libs/asio/doc/html/index.html. There's even no html folder in asio/doc. Is it just me? p.s., I certainly can find online documents at asio.sf.net, but it's not as convenient as having local files.
Hi Xu, On 22/03/07, Xu, Peng <peng.xu@intel.com> wrote:
I checked out boost HEAD recently and tried to use asio library. In folder PATH/boost/libs/asio, I did a "firefox index.html". It cannot find PATH/boost/libs/asio/doc/html/index.html. There's even no html folder in asio/doc. Is it just me?
It's not just you. Chris explained why the boost docs are temporarily gone here: http://article.gmane.org/gmane.comp.lib.boost.asio.user/448 HTH, Darren
Thanks Darren for filling me in. ________________________________ From: boost-users-bounces@lists.boost.org [mailto:boost-users-bounces@lists.boost.org] On Behalf Of Darren Garvey Sent: Thursday, March 22, 2007 5:23 PM To: boost-users@lists.boost.org Subject: Re: [Boost-users] [asio] broken link? Hi Xu, On 22/03/07, Xu, Peng <peng.xu@intel.com> wrote: I checked out boost HEAD recently and tried to use asio library. In folder PATH/boost/libs/asio, I did a "firefox index.html". It cannot find PATH/boost/libs/asio/doc/html/index.html. There's even no html folder in asio/doc. Is it just me? It's not just you. Chris explained why the boost docs are temporarily gone here: http://article.gmane.org/gmane.comp.lib.boost.asio.user/448 HTH, Darren
Actually, if you're working off boost CVS, and you have boostbook set up, you can build the docs yourself. Just run "bjam --v2" from the libs/asio/doc directory. Cheers, Chris
participants (5)
-
Christopher Kohlhoff
-
Darren Garvey
-
Joe Gottman
-
Olaf van der Spek
-
Xu, Peng