Boost C++ Libraries: Ticket #10337: weak_ptr & shared_ptr causes double "delete" -> crash https://svn.boost.org/trac10/ticket/10337 <p> I am not sure why this happens (VS2005 - VS2013) and specifically on windows (code compiles and runs fine on linux)... </p> <p> The following is the boost code that is responsible: </p> <blockquote> <p> void sp_counted_impl_p&lt;T&gt;::release() <em> nothrow { </em></p> <blockquote> <p> if( BOOST_INTERLOCKED_DECREMENT( &amp;use_count_ ) == 0 ) { </p> <blockquote> <p> dispose(); weak_release(); </p> </blockquote> <p> } </p> </blockquote> <p> } </p> </blockquote> <blockquote> <p> void sp_counted_impl_p&lt;T&gt;::weak_release() <em> nothrow { </em></p> <blockquote> <p> if( BOOST_INTERLOCKED_DECREMENT( &amp;weak_count_ ) == 0 ) { </p> <blockquote> <p> destroy(); </p> </blockquote> <p> } </p> </blockquote> <p> } </p> </blockquote> <p> Look what MUST happen, when "use_count = 1" and "weak_count = 1".. It causes a double release which crashes the application. That was a real pain in the ass to track down and unfortunately I have nothing to specifically trigger this issue either. Just look at the code and it should be obvious that this is a race condition. </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/10337 Trac 1.4.3 anonymous Sun, 10 Aug 2014 16:41:19 GMT <link>https://svn.boost.org/trac10/ticket/10337#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/10337#comment:1</guid> <description> <p> Yeah I can now tell this issue persists in 1.56... Also I found a bug in my code. The issue was that I had two shared_ptr of which one was using a weak_ptr to the other. Then the one the weak_ptr pointed to got release by C++ before the other, so that the race condition above happened. I still think this should be fixed, because its obviously a race condition in boost too. </p> <p> I solved the issue on my end by simply turning the weak_ptr into a shared_ptr and the original shared_ptr that the weak_ptr pointed to into a weak_ptr. This will force the correct release order at application exit upon boost and prevents the race condition from happening. </p> </description> <category>Ticket</category> </item> </channel> </rss>