Boost C++ Libraries: Ticket #1086: Serialization of weak_ptr produces invalid XML https://svn.boost.org/trac10/ticket/1086 <p> Hello, there seems to be a problem with serialization of weak_ptr. </p> <p> The second example below produces invalid XML where the same attributes appears two times with different values. It appears that both save and load using boost::serialization works, but other tools complains about the XML being invalid. We don't know if this is a problem in Boost.Serialization or in Boost.Archive </p> <p> The example has been tested using Boost 1.33.1 in Visual Studio 2005. We have not tested using Boost 1.34.0 yet. </p> <p> Anders Edin, Sidec Technologies AB </p> <hr /> <pre class="wiki"> #include &lt;iostream&gt; #include &lt;boost/shared_ptr.hpp&gt; #include &lt;boost/weak_ptr.hpp&gt; #include &lt;boost/archive/xml_oarchive.hpp&gt; #include &lt;boost/serialization/access.hpp&gt; #include &lt;boost/serialization/version.hpp&gt; #include &lt;boost/serialization/base_object.hpp&gt; #include &lt;boost/serialization/shared_ptr.hpp&gt; #include &lt;boost/serialization/weak_ptr.hpp&gt; class A { public: A() {} virtual ~A() {} virtual void set_self(boost::shared_ptr&lt;A&gt; s) { self_ = s; } virtual boost::shared_ptr&lt;A&gt; get_self() const { return self_.lock(); } private: friend boost::serialization::access; template &lt;typename Archive&gt; void serialize(Archive&amp; archive, const unsigned int file_version) { archive &amp; boost::serialization::make_nvp("self_", self_); } boost::weak_ptr&lt;A&gt; self_; }; BOOST_CLASS_VERSION(A, 1); class B: public A { public: B(): value_(3) {} virtual ~B() {} private: friend boost::serialization::access; template &lt;typename Archive&gt; void serialize(Archive&amp; archive, const unsigned int file_version) { archive &amp; BOOST_SERIALIZATION_BASE_OBJECT_NVP(A); archive &amp; boost::serialization::make_nvp("value_", value_); } int value_; }; BOOST_CLASS_VERSION(B, 1); int main(int argc, char* argv[]) { // This test object points to itself boost::shared_ptr&lt;A&gt; a(new A()); a-&gt;set_self(a); // This test object also points to itself, but by pointing to its base class boost::shared_ptr&lt;B&gt; b(new B()); b-&gt;set_self(b); // First example { boost::archive::xml_oarchive archive(std::cout); archive &lt;&lt; boost::serialization::make_nvp("A", a); } std::cout&lt;&lt;std::endl; // Second example { boost::archive::xml_oarchive archive(std::cout); archive &lt;&lt; boost::serialization::make_nvp("B", b); } return 0; } /* Output from running the program in Visual Studio 2005 -- The first example works as expected &lt;?xml version="1.0" encoding="UTF-8" standalone="yes" ?&gt; &lt;!DOCTYPE boost_serialization&gt; &lt;boost_serialization signature="serialization::archive" version="3"&gt; &lt;A class_id="0" tracking_level="0" version="1"&gt; &lt;px class_id="1" tracking_level="1" version="1" object_id="_0"&gt; &lt;self_ class_id="2" tracking_level="0" version="0"&gt; &lt;px class_id_reference="1" object_id_reference="_0"&gt;&lt;/px&gt; &lt;/self_&gt; &lt;/px&gt; &lt;/A&gt; &lt;/boost_serialization&gt; -- The second example produces invalid XML with attributes having two values in self_ &lt;?xml version="1.0" encoding="UTF-8" standalone="yes" ?&gt; &lt;!DOCTYPE boost_serialization&gt; &lt;boost_serialization signature="serialization::archive" version="3"&gt; &lt;B class_id="0" tracking_level="0" version="1"&gt; &lt;px class_id="1" tracking_level="1" version="1" object_id="_0"&gt; &lt;A class_id="2" tracking_level="1" version="1" object_id="_1"&gt; &lt;self_ class_id="3" tracking_level="0" version="0" class_id="4" tracking_level="0" version="1"&gt; &lt;px class_id_reference="1" object_id_reference="_0"&gt;&lt;/px&gt; &lt;/self_&gt; &lt;/A&gt; &lt;value_&gt;3&lt;/value_&gt; &lt;/px&gt; &lt;/B&gt; &lt;/boost_serialization&gt; */ </pre> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/1086 Trac 1.4.3 Eric Niebler Wed, 11 Jul 2007 17:14:56 GMT owner set https://svn.boost.org/trac10/ticket/1086#comment:1 https://svn.boost.org/trac10/ticket/1086#comment:1 <ul> <li><strong>owner</strong> set to <span class="trac-author">Robert Ramey</span> </li> </ul> Ticket Robert Ramey Fri, 30 Nov 2007 21:24:10 GMT status changed; resolution set https://svn.boost.org/trac10/ticket/1086#comment:2 https://svn.boost.org/trac10/ticket/1086#comment:2 <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">fixed</span> </li> </ul> <p> fixed by replacing tag name with "shared_ptr" </p> Ticket