[1.41][serialization] deserialization of weak_ptr fails
The example below works in 1.40 and fails in 1.41 #include <string> #include <sstream> #include <vector> #include <boost/smart_ptr.hpp> #include <boost/serialization/serialization.hpp> #include <boost/serialization/shared_ptr.hpp> #include <boost/serialization/vector.hpp> #include <boost/archive/xml_oarchive.hpp> #include <boost/archive/xml_iarchive.hpp> struct X : boost::enable_shared_from_this<X> { X(){} virtual ~X() {} explicit X(int i) : i(i) {} template<class Archive> void serialize(Archive & ar, const unsigned int version) { ar & boost::serialization::make_nvp("i", i); (void) version; } int i; }; typedef boost::shared_ptr<X> PX; typedef std::vector<PX> VPX; int main() { std::ostringstream oss; VPX vpx; vpx.push_back(PX(new X(2))); vpx.push_back(PX(new X(5))); vpx.push_back(PX(new X(7))); boost::archive::xml_oarchive oa(oss); oa << boost::serialization::make_nvp("v", vpx); vpx.clear(); std::string s = oss.str(); std::istringstream iss(s); boost::archive::xml_iarchive ia(iss); ia >> boost::serialization::make_nvp("v", vpx); PX px = vpx[1].get()->shared_from_this(); return 0; }
#include <string> #include <sstream> #include <boost/smart_ptr.hpp> #include <boost/serialization/serialization.hpp> #include <boost/serialization/shared_ptr.hpp> #include <boost/archive/xml_oarchive.hpp> #include <boost/archive/xml_iarchive.hpp> struct X : boost::enable_shared_from_this<X> { X(){} virtual ~X() {} explicit X(int i) : i(i) {} template<class Archive> void serialize(Archive & ar, const unsigned int) { ar & boost::serialization::make_nvp("i", i); } int i; }; typedef boost::shared_ptr<X> PX; int main() { std::stringstream ss; boost::archive::xml_oarchive oa(ss); PX px (new X(4)); oa << boost::serialization::make_nvp("v", px); px.reset(); ss.seekp(0); boost::archive::xml_iarchive ia(ss); ia >> boost::serialization::make_nvp("v", px); PX px1 = px.get()->shared_from_this(); }
Normally, one would expect so serialize a base class using the base object as below: However, serialization for enable_shared_from_this has not been written, so this won't work until that happens. A few minutes spent looking into this reveals that implementing serialization for this class is not a trivial task. So, for now, this cannot be done. Robert Ramey Alexander Gutenev wrote:
The example below works in 1.40 and fails in 1.41
struct X : boost::enable_shared_from_this<X> { X(){} virtual ~X() {} explicit X(int i) : i(i) {}
template<class Archive> void serialize(Archive & ar, const unsigned int version) { ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP( boost::enable_shared_from_this<X> ); ar & boost::serialization::make_nvp("i", i); }
int i; };
Are you sure this need special handling? I think the purpose of enable_shared_from_this is to make weak reference to self and maintain it automatically. You don't do anything special when constructing a shared_ptr with enable_shared_from_this-derived or when calling make_shared with it. And before 1.41 everything worked just fine! "Robert Ramey" <ramey@rrsd.com> wrote in message news:hg60vt$j5u$1@ger.gmane.org...
Normally, one would expect so serialize a base class using the base object as below:
However, serialization for enable_shared_from_this has not been written, so this won't work until that happens. A few minutes spent looking into this reveals that implementing serialization for this class is not a trivial task. So, for now, this cannot be done.
Robert Ramey
participants (2)
-
Alexander Gutenev
-
Robert Ramey