|
Boost Users : |
Subject: [Boost-users] [serialization] map of shared_ptr
From: Sebastian Messerschmidt (sebastian.messerschmidt_at_[hidden])
Date: 2013-10-03 05:52:51
Hi folks,
I stumbled over a strange behavior in the serialization.
Basically I want to write multiple entries into the same archive. The
data is a map of an index and some shared_ptr for the values.
Writing/reading are working but produce wrong results with the provided
minimal example.
I'm using boost 1.49 under Visual Studio 2010 64bit.
I've also tried the code without the use of shared_ptr and without a
map. Both version are working fine. The combination however fails.
What am I doing wrong?
cheers
Sebastian
<snip>
#include <map>
#include <fstream>
#include <boost/shared_ptr.hpp>
#include <boost/serialization/access.hpp>
#include <boost/serialization/map.hpp>
#include <boost/serialization/shared_ptr.hpp>
#include <boost/archive/binary_iarchive.hpp>
#include <boost/archive/binary_oarchive.hpp>
struct Test
{
template <class Archive>
void serialize(Archive& ar, const unsigned int /*version*/)
{
ar & c;
}
Test()
:c(0)
{}
int c;
};
typedef std::map<int, boost::shared_ptr<Test>> TestMap;
void save(const TestMap& m)
{
static std::ofstream os("d:/tmp/somedata.bin", std::ios_base::binary);
static boost::archive::binary_oarchive oa(os);
oa << m;
}
void load(TestMap& m)
{
static std::ifstream is("d:/tmp/somedata.bin", std::ios_base::binary);
static boost::archive::binary_iarchive ia(is);
ia >> m;
}
int main(int argc, char** argv)
{
TestMap t;
t.insert(std::make_pair(1, new Test()));
//save
#if 1
for (unsigned int i = 0; i< 100; ++i)
{
t[1]->c++;
save(t);
std::cout << t[1]->c << std::endl;
}
#else
//load
//This fails, c will always be the first serialized value
for (unsigned int i = 0; i< 100; ++i)
{
load(t);
std::cout << t[1]->c << std::endl;
}
#endif
}
</snip>
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