Boost logo

Boost Users :

From: James Conrad (james_at_[hidden])
Date: 2005-06-07 19:23:24


I'm evaluating the Boost.Serialization for use internal use as an
interchange format between a number of loose knit projects, and I've run
into a bit of trouble.

I'm using Vc 7.1 in Visual Studio .NET 2003.

It seems to me that, if I serialize an object first in an std::vector,
and then serialize it later by pointer, that there is trouble reading it
back.

I boiled down the reproduction case to as little code as possible below.

When newPolygon (below) is loaded from the archive, I get a bogus
pointer for m_pLastVertex. If I serialize using a binary archive, I hit
an assert in basic_iarchive.cpp line 371:

        assert(new_cid == cid);

Where cid is 576 and new_cid is 2.

Any help with this would be appriciated,

Thanks in advance,
-james conrad

#include <fstream>

#define BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP

#include <boost/archive/xml_oarchive.hpp>
#include <boost/archive/xml_iarchive.hpp>

#include <boost/serialization/vector.hpp>
#include <boost/serialization/export.hpp>

class Vector3
{
public:
    Vector3() {}
    Vector3( float x, float y, float z ) : _x(x), _y(y), _z(z) {}

    friend class boost::serialization::access;
    template<class Archive>
    void serialize(Archive & ar, const unsigned int version)
    {
        ar & BOOST_SERIALIZATION_NVP(_x);
        ar & BOOST_SERIALIZATION_NVP(_y);
        ar & BOOST_SERIALIZATION_NVP(_z);
    }

    float _x,_y,_z;
};

BOOST_CLASS_EXPORT_GUID(Vector3, "Vector3")

class Polygon
{
public:
    Polygon() : m_pLastVertex( NULL ) {}

    friend class boost::serialization::access;
    template<class Archive>
    void serialize(Archive & ar, const unsigned int version)
    {
        ar & BOOST_SERIALIZATION_NVP(m_vertices);
        ar & BOOST_SERIALIZATION_NVP(m_pLastVertex);
    }

    void addVertex( const Vector3& vert )
    {
        m_vertices.push_back( vert );
        m_pLastVertex = &m_vertices.back();
    }

    std::vector<Vector3> m_vertices;
    Vector3* m_pLastVertex;
};

BOOST_CLASS_EXPORT_GUID(Polygon, "Polygon")

int main( int argc, char* argv[] )
{
    // create class instance
    Polygon poly;
    poly.addVertex( Vector3( 0.0f, 1.0f, 0.0f ) );
    poly.addVertex( Vector3( 1.0f, 0.12565f, 0.2f ) );
    poly.addVertex( Vector3( 15.0f, 1.22f, 6.7f ) );
    poly.addVertex( Vector3( 1.43f, 1.55f, 2.5f ) );

    // create and open a character archive for output
    std::ofstream ofsx( "testout.xml" );
    boost::archive::xml_oarchive oax( ofsx );

    // write class instance to archive
    oax << BOOST_SERIALIZATION_NVP(poly);

    // close archive
    ofsx.close();

    Polygon newPoly;

    // create and open an archive for input
    std::ifstream ifs("testout.xml", std::ios::binary);
    boost::archive::xml_iarchive ia(ifs);
    // read class state from archive
    ia >> BOOST_SERIALIZATION_NVP( newPoly );
    // close archive
    ifs.close();

    return 0;
}

Here is an example of the .xml output:

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE boost_serialization>
<boost_serialization signature="serialization::archive" version="3">
<poly class_id="0" tracking_level="1" version="0" object_id="_0">
    <m_vertices class_id="1" tracking_level="0" version="0">
        <count>4</count>
        <item class_id="2" tracking_level="1" version="0" object_id="_1">
            <_x>0</_x>
            <_y>1</_y>
            <_z>0</_z>
        </item>
        <item object_id="_2">
            <_x>1</_x>
            <_y>0.12565</_y>
            <_z>0.2</_z>
        </item>
        <item object_id="_3">
            <_x>15</_x>
            <_y>1.22</_y>
            <_z>6.6999998</_z>
        </item>
        <item object_id="_4">
            <_x>1.4299999</_x>
            <_y>1.55</_y>
            <_z>2.5</_z>
        </item>
    </m_vertices>
    <m_pLastVertex class_id_reference="2"
object_id_reference="_4"></m_pLastVertex>
</poly>


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