Boost C++ Libraries: Ticket #5226: weak_ptr in program segfaults after expiring from shared_ptr from dynamically loaded shared library which was destructed and unloaded https://svn.boost.org/trac10/ticket/5226 <p> I have shared libraries which return shared_ptr&lt;Foo&gt; to my main program. </p> <p> The main program creates weak_ptr&lt;Foo&gt; from these shared pointers. </p> <p> Then the shared_ptr&lt;Foo&gt; objects are destructed (explicitly) and the shared library is unloaded. </p> <p> After that, expired() calls to the weak_ptr&lt;Foo&gt; return true, destructing the weak_ptr&lt;Foo&gt; segfaults. </p> <p> I have the suspicion that this is because weak_ptr uses a part of shared_ptr (this shared_count) that is supposed to survive the shared_ptr destruction. However, unloading the library invalidates the memory region where the code of that shared_count is located, so destructing the shared_weak_count fails? </p> <p> Creating a minimal example is on my TODO list, but perhaps you can already tell me whether this is expected, and whether a fix is even possible without a major change to the weak/shared ptr architecture. </p> <p> Thanks! </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/5226 Trac 1.4.3 Peter Schüller <schueller.p@…> Thu, 24 Feb 2011 14:47:17 GMT summary changed https://svn.boost.org/trac10/ticket/5226#comment:1 https://svn.boost.org/trac10/ticket/5226#comment:1 <ul> <li><strong>summary</strong> <span class="trac-field-old">weak_ptr in program segfaults after expiring from shared_ptr which was destructed by unloading shared library</span> → <span class="trac-field-new">weak_ptr in program segfaults after expiring from shared_ptr from dynamically loaded shared library which was destructed and unloaded</span> </li> </ul> <p> fixed the wrong title of the issue </p> Ticket Peter Dimov Thu, 24 Feb 2011 15:34:53 GMT <link>https://svn.boost.org/trac10/ticket/5226#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/5226#comment:2</guid> <description> <p> This is expected, sorry. Creating the shared_ptr in the shared library instantiates a vtable, which is accessed when the last weak_ptr is destroyed. One workaround is to expose a C interface from the shared library: </p> <p> T * create(); void destroy( T* ); </p> <p> and create the shared_ptr in an inline wrapper on the program side: shared_ptr&lt;T&gt;( create(), destroy ). </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Peter Dimov</dc:creator> <pubDate>Wed, 23 Mar 2011 00:16:24 GMT</pubDate> <title>status changed; resolution set https://svn.boost.org/trac10/ticket/5226#comment:3 https://svn.boost.org/trac10/ticket/5226#comment:3 <ul> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">invalid</span> </li> </ul> Ticket