I've had the same problems. The problem is with the bool
field. Code restoring the archive crashes if the bool stored in the archive is
not equal to 0 or 1. But since you did not write a constructor in the Obj class,
the boolean will actually be initialized with anything( eg, read 14675002 for
example), and will be serialized that way in the archive since it seems the
serialization code does not check if the bool is equal to 0 or 1 (only the
restoration code).
So, the solution is to initialize your boolean correctly
and be very careful whenever you use booleans. However, I think the
Serialization code should be fixed (remove the check for 0 or 1, it
probably does more bad than good.
Jean-Noël
The following code crashes under cygwin+boost1.33 and under
mingw+boost1.33
I create text archive in a file, and then serialization
crashes when restoring object from that archive.
The problem seem to be with
bool field, if I replace
bool
x;
with
int x;
in the class Obj implementation,
everything runs fine.
Is this a bug in serialization
code?
#include <iostream>
#include <sstream>
#include
<fstream>
#include <vector>
#include <map>
using
namespace std;
#include
<boost/archive/text_oarchive.hpp>
#include
<boost/archive/text_iarchive.hpp>
#include
<boost/serialization/vector.hpp>
#include
<boost/serialization/map.hpp>
using namespace boost;
using
namespace boost::serialization;
class Obj {
friend
class boost::serialization::access;
template<class
Archive>
void serialize(Archive & ar, const
unsigned int version)
{
ar & x;
}
public:
bool x;
};
class Item;
class
Cheq : public Obj{
friend class
boost::serialization::access;
template<class
Archive>
void serialize(Archive & ar, const
unsigned int version)
{
ar &
boost::serialization::base_object<Obj>(*this);
ar & v;
ar &
id;
}
public:
vector<Item> v;
int id;
Cheq(){}
};
class Item{
friend class
boost::serialization::access;
template<class
Archive>
void serialize(Archive & ar, const
unsigned int version)
{
ar & a;
ar &
s;
}
public:
int
a;
string s;
};
class Row{
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive &
ar, const unsigned int version)
{
ar & m1;
ar
& m2;
}
public:
map<string,string> m1,m2;
};
class Data{
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive &
ar, const unsigned int version)
{
ar & a;
ar &
m;
ar &
cheque;
}
public:
int a;
map<string,string> m;
map<int,Cheq> cheque;
};
BOOST_CLASS_TRACKING(Data,
boost::serialization::track_never)
#include
"boost/serialization/export.hpp"
BOOST_CLASS_EXPORT(Data);
BOOST_CLASS_EXPORT(Cheq);
BOOST_CLASS_EXPORT(Item);
int main(int
argc, char** argv)
{
Data d;
d.cheque[0]=Cheq();
//if(argc>1 &&
argv[1][0]=='1')
{
ofstream os("datfile");
boost::archive::text_oarchive oar(os);
oar << d;
}
//if(argc>1 &&
argv[1][0]=='2')
{
ifstream is("datfile");
boost::archive::text_iarchive iar(is);
iar >> d;
}
cout << "without crash\n";
return 0;
}
--
WBR, Pavlo Korzhyk
ICQ#155870780