Re:[boost] [serialization] exception during deserialization

Francois wrote:
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 would expect and "unregistered_class" exception on both serialization and de-serialization. If objects are to be serialized through a base class pointer, they must either: a) Be previously serialized directly or through the derived class pointer. or b) be "forward registered" with the archive. Or c) be "exported" via BOOST_CLASS_EXPORT
template<class Archive> void serialize(Archive & ar, const unsigned int version) { ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(Base); // What is the intended purpose for the following? // I can't see why one would want to do this. /* uncomment to avoid the exception * ar & BOOST_SERIALIZATION_NVP(this); */ } };
Robert Ramey

Robert Ramey wrote: ...
If objects are to be serialized through a base class pointer, they must either:
a) Be previously serialized directly
If I understand what you mean by "serialized directly", that is what I am trying to do.
or through the derived class pointer.
This way it works. This is the meaning of "ar & BOOST_SERIALIZATION_NVP(this)" in the code at the end. It is semantically meaningless, but it generates a reference to the object with the pointer of the right type. Hopefully it does not do anything bad when deserializing.
template<class Archive> void serialize(Archive & ar, const unsigned int version) { ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(Base); // What is the intended purpose for the following? // I can't see why one would want to do this. /* uncomment to avoid the exception * ar & BOOST_SERIALIZATION_NVP(this); */ } };
François Fath
participants (2)
-
FATH Francois
-
Robert Ramey