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

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>


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

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

class Vector3
    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)

    float _x,_y,_z;


class Polygon
    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;


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

    // close archive

    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

    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">
        <item class_id="2" tracking_level="1" version="0" object_id="_1">
        <item object_id="_2">
        <item object_id="_3">
        <item object_id="_4">
    <m_pLastVertex class_id_reference="2"

Boost-users list run by williamkempf at, kalb at, bjorn.karlsson at, gregod at, wekempf at