Boost logo

Boost Users :

From: Toby Smith (tob_at_[hidden])
Date: 2006-01-30 16:40:44


Hi,

I'm having a problem serializing a multi-index container. Serialization
used to work fine, until I added a second (non-unique) key to the
container. I've managed to distill the problem down to a small bit of
code, so here it is. Uncomment the #define to see the segfault at
serialization time in action, leave it commented and the example code
works fine. I'm clearly missing something subtle (or not-so-subtle) here,
but I don't have the first clue what it might be. Suggestions are
enormously appreciated.

Regards,
tob

----------------------------------------------------------------------------
// A simple program to illustrate my crazy serialization problem with
// multi-index containers. Compiled with:
// g++ -I/usr/local/include/boost-1_33_1/ boost_test.c++ \
// -lboost_serialization -o boost_test
//
// When two keys are specified for FooSet, serialization segfaults. When
// only one key is present, we're fine. Uncomment the line below to see
// the segfault in action.

// #define KILL_SERIALIZATION

#include <fstream>
#include <string>

#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/member.hpp>
#include <boost/multi_index/ordered_index.hpp>

struct Foo
{
    Foo() : _id(0) { }
    Foo(unsigned id) : _id(id) { }

    unsigned _id;
    std::string _name;

    template<class Archive>
    void serialize(Archive & ar, const unsigned int version) {
        ar & _id;
        ar & _name;
    }
};

// Container for class Foo
struct foo_id_tag { };
struct foo_name_tag { };

typedef boost::multi_index_container<
    Foo,
    boost::multi_index::indexed_by<
        boost::multi_index::ordered_unique<
            boost::multi_index::tag<foo_id_tag>,
            BOOST_MULTI_INDEX_MEMBER(Foo, unsigned, _id)
#if defined(KILL_SERIALIZATION)
>,
        boost::multi_index::ordered_non_unique<
            boost::multi_index::tag<foo_name_tag>,
            BOOST_MULTI_INDEX_MEMBER(Foo, std::string, _name)
#endif
>
>
> FooSet;

void save(const FooSet& fooSet)
{
    std::ofstream ofs("foo");
    boost::archive::text_oarchive oa(ofs);
    oa << fooSet;
}

int main(int argc, char** argv)
{
    FooSet foo_set;

    foo_set.insert(Foo(100));
    foo_set.insert(Foo(150));

    save(foo_set);

    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