Boost logo

Boost :

From: FATH Francois (francois.fath-renexter_at_[hidden])
Date: 2004-05-07 04:09:59


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;
}


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk