Opened 12 years ago

Last modified 9 years ago

#5299 new Bugs

Serialization throws an exception for loading binary archive on windows

Reported by: Wang Peng <wangp.thu@…> Owned by: Robert Ramey
Milestone: To Be Determined Component: serialization
Version: Boost 1.46.0 Severity: Problem
Keywords: serialization windows binary load Cc:

Description

Serialization throws an exception for loading binary archive on windows. THere is no error when using text or xml archive. No error on Ubuntu when using binary, text or xml archive.

I have tested the cpp file on both Window XP-x86 and Window 7-x64. The errors are the same.

A minimal and complete example is the following: (in s11n_bug.cpp):

#include <fstream>

#include <boost/archive/binary_iarchive.hpp>
#include <boost/archive/binary_oarchive.hpp>
#include <boost/serialization/vector.hpp>
#include <boost/serialization/shared_ptr.hpp>
#include <boost/serialization/nvp.hpp>

typedef std::vector<int> B;

class A;

typedef boost::shared_ptr<A> a_ptr;
typedef boost::shared_ptr<B> b_ptr;

class A{
public:
	A(){}
	A(b_ptr b) : b_(b){}
  virtual ~A(){}
  b_ptr b_;

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

class DA : public A{
public:
	DA(){}
	DA(b_ptr b) : A(b){}
	template<class Archive>
  void serialize(Archive & ar, const unsigned int version){
		ar & boost::serialization::make_nvp("base", boost::serialization::base_object<A>(*this));
  }
};

typedef std::vector<a_ptr> VA;
typedef boost::shared_ptr<VA> va_ptr;

typedef std::vector<va_ptr> VVA;
typedef boost::shared_ptr<VVA> vva_ptr;

template <typename Archive>
void register_types(Archive& ar){
  ar.template register_type<DA>();
}

b_ptr make_b(){
	return b_ptr(new B);
}

a_ptr make_a(){
	return a_ptr(new DA(make_b()));
}

va_ptr make_va(){
	VA va;
	va.push_back(make_a());
	va.push_back(make_a());
	return va_ptr(new VA(va));
}

vva_ptr make_vva(){
	VVA vva;
	vva.push_back(make_va());
	vva.push_back(make_va());
	return vva_ptr(new VVA(vva));
}

int main(){
	std::string filename = "tmp";
	std::vector<vva_ptr> thing;
	thing.push_back(make_vva());
	thing.push_back(make_vva());
	{
		std::ofstream ofs(filename.c_str());
		boost::archive::binary_oarchive oa(ofs);
		register_types(oa);
		oa << BOOST_SERIALIZATION_NVP(thing);
	}
	{
		std::ifstream ifs(filename.c_str());
		boost::archive::binary_iarchive ia(ifs);
		register_types(ia);
		ia >> BOOST_SERIALIZATION_NVP(thing);
	}
	return 0;
}

Attachments (1)

s11n_bug.cpp (1.9 KB ) - added by Wang Peng <wangp.thu@…> 12 years ago.
minimal example

Download all attachments as: .zip

Change History (3)

by Wang Peng <wangp.thu@…>, 12 years ago

Attachment: s11n_bug.cpp added

minimal example

comment:1 by polifa@…, 9 years ago

Is there any news on this? I'm getting the same error using boost-1.54 :O

Thanks, Fabio

in reply to:  1 comment:2 by polifa@…, 9 years ago

Replying to polifa@…:

Is there any news on this? I'm getting the same error using boost-1.54 :O

Thanks, Fabio

I've just figure out the problem. Streams have to be opened with the std::fstream::binary flag.

Note: See TracTickets for help on using tickets.