Boost C++ Libraries: Ticket #9567: De-serialization of enable_shared_from_this through base class pointer fails https://svn.boost.org/trac10/ticket/9567 <p> Given the following class structure: </p> <blockquote> <p> Base is a simple class. Derived inherits from Base. Derived also inherits from boost::enable_shared_from_this&lt;Derived&gt;. Base and Derived implement boost::serialization. </p> </blockquote> <p> When deserializing an instance of Derived into a vector of boost::shared_ptr&lt;Derived&gt;: </p> <blockquote> <p> Base and Derived members are reconstructed correctly. Derived::shared_from_this() works as expected. </p> </blockquote> <p> But when deserializing an instance of Derived into a vector of boost::shared_ptr&lt;Base&gt;: </p> <blockquote> <p> Base and Derived members are still reconstructed correctly. Derived::shared_from_this() throws a bad_weak_ptr exception. This is because enable_shared_from_this::weak_ptr is NOT reconstructed - It is zero. </p> </blockquote> <p> See test code in attached file that demonstrates both of the above cases. </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/9567 Trac 1.4.3 t_regan@… Mon, 13 Jan 2014 10:55:57 GMT attachment set https://svn.boost.org/trac10/ticket/9567 https://svn.boost.org/trac10/ticket/9567 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">test_shared_from_this.cpp</span> </li> </ul> <p> Test demonstrating enable_shared_from_this deserialization problem </p> Ticket t_regan@… Tue, 14 Jan 2014 12:18:38 GMT <link>https://svn.boost.org/trac10/ticket/9567#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/9567#comment:1</guid> <description> <p> Correction to original description: enable_shared_from_this::weak_this_::px is NOT reconstructed - It is zero. </p> </description> <category>Ticket</category> </item> <item> <author>t_regan@…</author> <pubDate>Tue, 14 Jan 2014 12:24:04 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/9567#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/9567#comment:2</guid> <description> <p> Workaround: weak_this_ can be reconstructed by calling _internal_accept_owner (which fortunately is public) on each deserialized instance of Derived. </p> </description> <category>Ticket</category> </item> <item> <author>t_regan@…</author> <pubDate>Tue, 14 Jan 2014 12:30:36 GMT</pubDate> <title>attachment set https://svn.boost.org/trac10/ticket/9567 https://svn.boost.org/trac10/ticket/9567 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">test_shared_from_this.2.cpp</span> </li> </ul> <p> Added test case to demonstrate workaround </p> Ticket Robert Ramey Tue, 04 Feb 2014 01:16:37 GMT <link>https://svn.boost.org/trac10/ticket/9567#comment:3 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/9567#comment:3</guid> <description> <p> I compiled and ran your example on my machine and have reproduced the problem. </p> <p> Looking at your example, it seems to me that the serialization function of Derived should be: </p> <pre class="wiki"> template&lt;class Archive&gt; void serialize(Archive &amp; ar, const unsigned int version) { ar &amp; BOOST_SERIALIZATION_BASE_OBJECT_NVP(Base); ar &amp; boost::serialization::make_nvp( enable_shared_from_this, boost::serialization::base_object&lt;boost::enable_shared_from_this&lt;Derived&gt; &gt;(*this) ) ar &amp; BOOST_SERIALIZATION_NVP(m_derived); } </pre><p> Of course this begs the question as to how serialization for boost::enable_shared_from_this&lt;Derived&gt; should be implemented. Of hand I don't remember what boost::enable_shared_from_this&lt;Derived&gt;does right now. ( could guess but I'm not going to waste your time). So my advice is to figure out how to implement serialization for boost::enable_shared_from_this&lt;Derived&gt; and re-submit. </p> <p> maybe something like the following would work </p> <pre class="wiki"> template&lt;class Archive&gt; void serialize(Archive &amp; ar, const unsigned int version) { boost_shared_ptr&lt;Derived&gt; m_sp = shared_from_this; ar &amp; BOOST_SERIALIZATION_BASE_OBJECT_NVP(Base); ar &amp; BOOST_SERIALIZATION_NVP(m_sp); ar &amp; BOOST_SERIALIZATION_NVP(m_derived); } </pre> </description> <category>Ticket</category> </item> <item> <dc:creator>Robert Ramey</dc:creator> <pubDate>Thu, 27 Feb 2014 17:50:39 GMT</pubDate> <title>attachment set https://svn.boost.org/trac10/ticket/9567 https://svn.boost.org/trac10/ticket/9567 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">test_enable_shared_from_this.cpp</span> </li> </ul> Ticket Robert Ramey Thu, 10 Jul 2014 13:53:20 GMT status changed; resolution set https://svn.boost.org/trac10/ticket/9567#comment:4 https://svn.boost.org/trac10/ticket/9567#comment:4 <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">wontfix</span> </li> </ul> Ticket