[serialization] exception during deserialization

I am trying to use the serialization library to save an object and a pointer to the object, but the pointer has the type of the object's base class. Something like: // declaration MyClass myObject; // object, not pointer MyBaseClass *myPointer = &myObject; // serialization code ar & BOOST_SERIALIZATION_NVP(myObject); ar & BOOST_SERIALIZATION_NVP(myPointer); And I get an exception (either null reference exception or access violation) during the deserialization. I am using xml archives. The xml generated code seems fine. I have also noticed that saving a pointer of the object's type pointing to the object solves the problem: // updated code MyClass myObject; // object, not pointer MyClass *myOtherPointer = & myObject MyBaseClass *myPointer = &myObject; // serialization code ar & BOOST_SERIALIZATION_NVP(myObject); ar & BOOST_SERIALIZATION_NVP(myOtherPointer); ar & BOOST_SERIALIZATION_NVP(myPointer); So is it a bug, or have I done something wrong ? The compiler is VC7.1, full example code is in attachement. Thanks, Francois Fath -- Disclaimer ------------------------------------ Ce message ainsi que les eventuelles pieces jointes constituent une correspondance privee et confidentielle a l'attention exclusive du destinataire designe ci-dessus. Si vous n'etes pas le destinataire du present message ou une personne susceptible de pouvoir le lui delivrer, il vous est signifie que toute divulgation, distribution ou copie de cette transmission est strictement interdite. Si vous avez recu ce message par erreur, nous vous remercions d'en informer l'expediteur par telephone ou de lui retourner le present message, puis d'effacer immediatement ce message de votre systeme. *** This e-mail and any attachments is a confidential correspondence intended only for use of the individual or entity named above. If you are not the intended recipient or the agent responsible for delivering the message to the intended recipient, you are hereby notified that any disclosure, distribution or copying of this communication is strictly prohibited. If you have received this communication in error, please notify the sender by phone or by replying this message, and then delete this message from your system. #include "stdafx.h" #include <fstream> #include <boost/archive/xml_oarchive.hpp> #include <boost/archive/xml_iarchive.hpp> #include <boost/serialization/export.hpp> using namespace std; /* * Base class */ class Base { public: Base() {} virtual ~Base() {} private: friend class boost::serialization::access; template<class Archive> void serialize(Archive & ar, const unsigned int version) { } }; /* * Derived class */ class Derived : public Base { public: Derived() {} virtual ~Derived() {} private: friend class boost::serialization::access; template<class Archive> void serialize(Archive & ar, const unsigned int version) { ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(Base); /* uncomment to avoid the exception * ar & BOOST_SERIALIZATION_NVP(this); */ } }; /* * exports */ BOOST_CLASS_EXPORT(Base) BOOST_CLASS_EXPORT(Derived) int _tmain(int argc, _TCHAR* argv[]) { // create Derived derived; Base *base = &derived; // serialize { ofstream ofs("toto.xml"); boost::archive::xml_oarchive oa(ofs); oa << BOOST_SERIALIZATION_NVP(derived); oa << BOOST_SERIALIZATION_NVP(base); } // deserialize { ifstream ifs("toto.xml"); boost::archive::xml_iarchive ia(ifs); ia >> BOOST_SERIALIZATION_NVP(derived); ia >> BOOST_SERIALIZATION_NVP(base); } //quit return 0; }

Hello,
I am trying to use the serialization library to save an object and a pointer to the object, but the pointer has the type of the object's base class.
// declaration MyClass myObject; // object, not pointer MyBaseClass *myPointer = &myObject;
// serialization code ar & BOOST_SERIALIZATION_NVP(myObject); ar & BOOST_SERIALIZATION_NVP(myPointer);
I have also noticed that saving a pointer of the object's type pointing to the object solves the problem: ... So is it a bug, or have I done something wrong ?
This seems to be a bug in the current serialization library. I've tracked it down to the fact that no basic_pointer_iserializer was registered before the pointer to the derived class gets deserialized. As far as I can tell a basic_pointer_iserializer only gets registered when an object is loaded through a pointer. That's also why it works when you additionally save a pointer to the object. I managed to fix it in my local copy and got your sample application to run with it successfully. The fix is straightforward. When basic_archive_impl::load_object is called it not only needs to register a basic_iserializer but also a basic_pointer_iserializer, just as basic_archive_impl::load_pointer does. So I simply changed it to also receive a basic_pointer_iserializer which is instantiated in the load_non_pointer_type structure in iserializer.hpp. However, I'm not sure if this is a good fix and so we'll have to wait what Robert has to say about this. Best Regards, Martin TAB Austria Industrie- und Unterhaltungselektronik GmbH & CoKG http://www.tab.at
participants (2)
-
FATH Francois
-
martin.ecker@tab.at