Boost logo

Boost Users :

From: Terence Wilson (tez_at_[hidden])
Date: 2006-01-04 18:17:47


I have condensed a short example which is appended to this message. The code
tries to write then read a polymorphic class via a base pointer. The
assertion I am seeing is assert(NULL != bpos_ptr); in oserializer.hpp. The
documentation indicates that the problem is likely a lack of registration of
the class, however, I have done this. Interestingly the code only fails for
XML archive type.

// BoostPolymorphicSerialize.cpp : Defines the entry point for the console
application.
//

#include <stdio.h>
#include <tchar.h>

#include <iomanip>
#include <iostream>
#include <fstream>
#include <string>

#include <boost/archive/tmpdir.hpp>

#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>

#include <boost/serialization/base_object.hpp>
#include <boost/serialization/utility.hpp>
#include <boost/serialization/list.hpp>
#include <boost/serialization/export.hpp>

#include <boost/shared_ptr.hpp>
#include <boost/shared_array.hpp>
#include <boost/serialization/shared_ptr.hpp>

#include "boost/archive/xml_iarchive.hpp"
#include "boost/archive/xml_oarchive.hpp"

class CMyBaseClass
{
public:
        CMyBaseClass(void):m_BaseValue(0){}
public:
        virtual ~CMyBaseClass(void){}
        int m_BaseValue;

        friend class boost::serialization::access;
        template<class Archive> void serialize(Archive & ar, const unsigned
int /* file_version */)
        {
                ar & BOOST_SERIALIZATION_NVP(m_BaseValue);
        }
};

class CMyDerivedClass : public CMyBaseClass
{
public:
        CMyDerivedClass(void):m_DerivedValue(1){}
public:
        ~CMyDerivedClass(void){}
        int m_DerivedValue;

        friend class boost::serialization::access;
        template<class Archive> void serialize(Archive & ar, const unsigned
int /* file_version */)
        {
                BOOST_SERIALIZATION_BASE_OBJECT_NVP(CMyBaseClass);
                ar & BOOST_SERIALIZATION_NVP(m_DerivedValue);
        }
};

BOOST_CLASS_EXPORT_GUID(CMyBaseClass, "CMyBaseClass")
BOOST_CLASS_EXPORT_GUID(CMyDerivedClass, "CMyDerivedClass")

int _tmain(int argc, _TCHAR* argv[])
{
        const CMyBaseClass* const pMyBaseClass=new CMyDerivedClass();
        const std::string fileName("c:\\docs\\BoostSerializeTest.xml");
        CMyBaseClass* pMyBaseClass2=0;

        try
        {
                {
                        std::ofstream ofs(fileName.c_str());
                        assert(ofs.good());
                        boost::archive::xml_oarchive oa(ofs);
                        //boost::archive::text_oarchive oa(ofs);
                        oa << BOOST_SERIALIZATION_NVP(pMyBaseClass);
                }

                {
                        std::ifstream ifs(fileName.c_str());
                        assert(ifs.good());
                        boost::archive::xml_iarchive ia(ifs);
                        //boost::archive::text_iarchive ia(ifs);
                        ia >> BOOST_SERIALIZATION_NVP(pMyBaseClass2);
                }
        }
        catch(std::exception& e)
        {
                std::cout << e.what() << std::endl;
        }

        delete pMyBaseClass;
        delete pMyBaseClass2;

        return 0;
}


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net