Boost serializing loading fails with exception thrown - c++

I have been trying to make this work for a long time now.
In my project there are 6 classes that are being serialized using the exact tutorial from boost, by implementing the template function serialize.
Those classes are: State, guState, Policy, Action, Param, Vec3D.
When I serialize and save them, it works fine. I get indeed a text file, with various numbers and strings in it.
No complains, no warnings, no exceptions thrown. The only case is that if I try to serialize a pointer member of a class, the hole process becomes a zombie. So I do not try doing so, and saving works.
When however I try loading, I get:
terminate called after throwing an instance of
'boost::archive::archive_exception' what(): stream error
Now the interesting part is that I serialize two boost::ptr_vectors, one which consists of State pointers and one which consists of Policy pointers.
The state vector, I have saved and loaded without a problem.
The policy vector, I can save, but when I try loading i get the exception.
Furthermore, after reading the boost tutorials, I was under the impression that In order to load I did not need anything else other than the serialize function.
However when I tried loading, boost serialization would complain for not finding a default constructor such as State(), Policy(), etc (I implement my own constructors in each class ).
After reading this tutorial here I implemented a default constructor which does nothing, so that boost-serialization would work. Indeed it did compile, and I got the results mentioned above.
I have tried going down a much complicated road seen in my old question here where I tried seperating and implementing save_construct_data and load_construct_data, but I found this too instrusive, again I got the exact error as above.
Can someone please help me, explain how the loading works, what is the deal with the default constructors ? Or at least point me to a link that might be helpfull. I have gone through the manuals in boost, and they do not explain much about reconstruction.
Thank you.
Edit (Added a few snippets)
class State
{
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int version);
protected:
std::map<std::string,float> positions;
float reward;
std::size_t hash_value;
bool exists(const Action* A);
bool operator== (State const& S);
std::size_t hash();
void clean_noise();
State(){}; // NOTE: This is used only by serializer, without it, code won't compile
public:
enum position { standing, on_chest, on_back, on_left, on_right, getting_up };
position current_position;
Policy *my_policy;
Vec3D gps;
boost::ptr_vector<Action> actions;
State(ACTION_MODE &m);
~State();
bool operator== (State const* S);
bool operator< (State const* S) const ;
const float& getR() const;
bool addAction(Action *A);
Action* findAction(const Action *A);
boost::ptr_vector<Action>& getAllActions();
void printState();
virtual bool isTerm();
};
template <class Archive>
void State::serialize(Archive& ar, const unsigned int version)
{
ar & BOOST_SERIALIZATION_NVP(positions);
ar & BOOST_SERIALIZATION_NVP(gps);
ar & BOOST_SERIALIZATION_NVP(current_position);
ar & BOOST_SERIALIZATION_NVP(reward);
ar & BOOST_SERIALIZATION_NVP(hash_value);
ar & BOOST_SERIALIZATION_NVP(actions);
ar & BOOST_SERIALIZATION_NVP(my_policy);
}
Other Classes inheriting from State, have also their serialize functions, using:
ar & boost::serialization::base_object<State>(*this);
Class Policy:
class Policy
{
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int version);
Policy() {}; // NOTE: Again same as with state, used only by serialize load
protected:
float QValue;
State *state;
public:
//! Base class constructor
Policy(State *s);
...
};
template <class Archive>
void Policy::serialize(Archive& ar, const unsigned int version)
{
ar & BOOST_SERIALIZATION_NVP(action);
ar & BOOST_SERIALIZATION_NVP(state);
ar & BOOST_SERIALIZATION_NVP(QValue);
ar & BOOST_SERIALIZATION_NVP(r);
}
As you can see those are the two main classes, Other classes are also serialized because of reconstruction dependencies ( class Action, class Param, etc )
The master Class:
template <class S, class P> class Task
{
protected:
...
//! Container of states of type S (template parameter)
boost::ptr_vector<S> states;
//! Container of policies of type P (template parameter)
boost::ptr_vector<P> policies;
...
public:
Task(Agent &a, ACTION_MODE &m);
...
void save_to_file();
void load_from_file(std::string filename);
};
template <class S, class P>
void Task<S,P>::save_to_file()
{
std::string output = ramdisk+"serialized";
char *file = (char*)output.c_str();
std::ofstream ofs(file);
assert(ofs.good());
boost::archive::text_oarchive oa(ofs);
oa << states;
oa << policies;
ofs.close();
}
template <class S, class P>
void Task<S,P>::load_from_file(std::string filename)
{
char *file = (char*)output.c_str();
std::cout << file << std::endl;
std::ifstream ifs(file);
boost::archive::text_iarchive ia(ifs);
ia >> states;
ia >> policies;
ifs.close();
}
Effectively contains the two boost::ptr_vectors which hold the States and Policies.
States are saved and loaded without a problem.
The problem arises when loading policies. Saving them does not seem to create a problem (but then again I might be wrong).
Having tested save/load without policies, and with, The issue appears to be with policy reconstruction.
Note the default constructors used only by serialization, without which the code will not compile.
EDIT#2: After running application using valgrind and memcheck, it reports that there is a pointer memory leak. However since I am not good at debugging with valgrind I can't really tell where the leak is occuring or if it is relevant to my serialization (I think it is).

The problem is that you are serializing states and policies while Policy also holds references to the same instances of State. You can only serialize classes which don't have such cross-references. Boost should throw a pointer-conflict exception when writing to the file. In my tests it dependet on the order of writes if the exception was thrown or not - which is unfortunate because loading fails even if writing succeeded.
A workaround would be to delete the line oa << states when saving and loading and fix up the pointers by hand in a post-loading step.
About the constructors: it's mostly something the boost-api needs to do it's template magic. However when using versioning it's important to specify default-values for your member-variables so they are not left uninitialized when loading files with an older version-number.

Related

Build error: base specifier must name a class

I am trying to build a big project in Mac OS X with cmake and ran into following error which i am unable to solve.
Archive.hpp:92:30: error: base specifier must name a class
struct Derived : T, Fallback { };
Code:
template<typename T>
class has_save_func
{
struct Fallback { int save; }; // add member name "X"
struct Derived : T, Fallback { };
...
Furthermore i have following:
Archive.hpp:137:13: error: type 'unsigned long' cannot be used prior to '::'
Code:
template <class A>
static bool save(const A& data, class OutputArchive& oarchive, const std::string& id, typename boost::enable_if_c<has_save_func<A>::value, A>::type* def=NULL){
// todo check if A actually is friend with Access class, else return false
A::save(data, oarchive); // ! Error on this line !
return true;
}
template <class A>
static bool save(const A& data, class OutputArchive& oarchive, const std::string& id, typename boost::disable_if_c<has_save_func<A>::value, A>::type* def=NULL){
// todo check if A actually is friend with Access class, else return false
return serialization::save<A>( data, oarchive, id);
}
Code calling (OutputArchive.hpp):
template<class T>
void write(const T& data, const std::string& id){
// the data method must have an implementation of load/save and if not then we try the generic write
// method which could provide a solution by the implementation itself
writeEnterScope(id);
try {
Archive::Access::save<T>(data, *this, id);
} catch (...){
// we fall back to this call
boost::any adata(data);
write(adata, id);
}
writeLeaveScope(id);
}
Code serializeutil.cpp
void save(const rw::math::Q& tmp, OutputArchive& oar, const std::string& id){
oar.write(tmp.size(), "size");
for(int i=0;i<tmp.size();i++){
oar.write(tmp[i],"q");
}
}
Could it be a problem with the compiler im using?
Both errors point to the same: you are trying to use your templates with a non-class, most probably unsigned int. In the first case, you would be trying to have Derived inherit from unsigned int, which is illegal; in the second, you would be trying to call a static method (save()) on unsigned int, which is illegal again. Looking at the code calling the templates would clarify the issue.
UPDATE: From the information added to the question we can now conclude that this was indeed the case. tmp.size(), is most probably an unsigned int, so you are calling oar.write() with an unsigned int; this, in turn, calls save() with an unsigned int, so it tries to call unsigned int::save(), which is illegal, and instantiate class has_save_func<unsigned int>, which tries to define struct Derived : unsigned int, Fallback which is illegal again.
I'm afraid you will need to redesign your classes if you want them to work with built-in types, such as unsigned int. You might do a complete redesign or just overload functions write() or save(), depending on what you have available.
I think that i may be responsible for the mentioned pieces of code. Something is missing though, and multiple persons already noticed this. The overloaded write functions on the OutputArchive which currently looks something like this:
virtual void writeEnterScope(const std::string& id) = 0;
virtual void writeLeaveScope(const std::string& id) = 0;
virtual void writeEnterArray(const std::string& id) = 0;
virtual void writeLeaveArray(const std::string& id) = 0;
// writing primitives to archive
virtual void write(bool val, const std::string& id) = 0;
virtual void write(int val, const std::string& id) = 0;
virtual void write(unsigned int val, const std::string& id){ write((int)val,id); }
virtual void write(boost::uint64_t val, const std::string& id) = 0;
virtual void write(double val, const std::string& id) = 0;
virtual void write(const std::string& val, const std::string& id) = 0;
The serialization part of the software was not supposed to be used yet, but it ended up in the build system anyways. If you comment out the serialize directory in CMakeLists.txt in src/rwlibs then it should work. Or add a write function for an unsigned long:
virtual void write(unsigned long val, const std::string& id){};
And yes, i did look into Boost.Serialization before venturing into creating yet another serialization framework. I was however trying to create something that would be less intrusive, less templated and more user friendly.... Guess i failed at that...
First, it would be better to use existing solution like Boost.Serialization. It's already debugged and works in all the cases you may need.
However, you should still know where your current code has problems and how to do template machinery like this. So:
oar.write(tmp.size(), "size");
^^^^^^^^^^
This is unsigned int. And you do need to serialize it. So you need a write, that can accept primitive types. There are two options:
Write non-template overloads for primitive types. Non-template overloads have priority over template ones, so if you write explicit non-template overload with unsigned int first argument, the template won't be instantiated and there won't be any error. You will however need overloads for each possible primitive type separately, because template that matches exactly will be still preferred over non-template overload that requires conversion.
Use free save function instead of method. Advantage of method is that it can be virtual, but you don't usually need that with template. Advantages of free function are that they can be defined for non-class types and that they can be defined for already existing classes, both of which you often do need in templates. So you would change all instances of the save method to free function, drop the has_save_func altogether and overload the save function for primitive types you need.
Amend the has_save_func with check whether the template argument is a class type. Non-class types don't have methods, so that's what the other variant will do. You can either use the boost::is_class or implement something similar. Boost actually implements it by enumerating all the other options, but it can also be implemented using pointer-to-member, which will cause SFINAE when given non-class type. Unfortunately you don't have anything to cause SFINAE when given class type, so you have to combine with function templates and sizeof and end up with really tricky stuff (I'm sure I've seen it, but really don't remember it).

boost::serialization with member function and free function

I have a class mat4 from 3rdparty open source math library. It doesn't have own serialization method, so I've created a separate header in my main project that contains a serialization of matrix mat4:
namespace boost {
namespace serialization {
template<class Archive>
void serialize(Archive & ar, mat4 & matrix, const unsigned int version)
{
// some serialization of matrix goes here...
}
} // namespace serialization
} // namespace boost
It worked perfectly until later when I added serialization directly to class mat4 and forgot to remove old serialization:
struct mat4
{
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar & BOOST_SERIALIZATION_NVP(data);
}
// some other members and functions goes here
};
After such modification the free function serialize was called.
Could you please explain why the free function was called instead of mat4 member function? Probably, there is some simple rule in standard that I missed.
When I comment out the free function - member function is called.
The reason is due to the technique used by boost::serialization to allow both a free function & member functions. Basically there's a free function with the following signature:
template< typename Archive, typename Type >
void serialize( Archive& a_Arch, Type& a_Inst, const unsigned int a_Version )
{
a_Inst.serialize( a_Arch, a_Version );
}
When C++ considers a match for a function the one that is most specialized wins out, which means that any free function where Type is less generic, such as the user defined free serialization functions always win. If there is none, the above function wins out which is only well formed for Type with a serialize member that accepts an archive & version.

boost::serialization: overloading load_construct_data: no accessible constructor at all

so I'm using the boost::serialization library, and I'm trying to overide how a class is constructed, since it has no default constructor. This is demonstrated here. To me it appears the function takes the class* t and then sets it to point to a newly constructed object. If i'm wrong, this is definately the source of my error.
However, the only way to construct my class is by using another classes create() function, meaning I need to stray from the code in the example (this is stated in the boost::serialization namespace): ::new(t)my_class(attribute);
I tried simply calling the create function and setting t equal to the returned pointer, but this doesn't seem to work because right after the load_construct_data function, and in the serialization function, the given myClass& is not the same as what I set 't' to.
How do I do whatever ::new(t) is doing so the object created using the create function follows through into the serialize/other functions?
The construct referred to in your question (new(t) my_class(attribute)) is called "placement new". The way it work is that
t must already point to an allocated region of memory (placement new doesn't do allocation by default)
An instance of my_class is constructed at that memory location.
However, since in your case you can't use constructors, then using any form of new is out of the question. But there is an alternative (sort of).
Since placement new pretty much just overwrites a chunk memory, we can use a regular function which does the same with an already constructed object. Such a function is memcpy:
void * memcpy ( void * destination, const void * source, size_t num );
All memcpy does is perform a byte-wise copy of num bytes from the memory pointed to by source to the memory pointed to by destination.
So let's say you started with this code in the load_construct_data
my_class obj = other_class::create();
Then we can use the memcpy function to "move" the value at obj into the memory reference by t:
memcpy((void*)t, (void*)(&obj), sizeof(obj));
While there are some details about how this works with your particular class, such as whether a bit-wise copy is "good enough", this is the best I've got with what you've asked. The one problem I see is if the destructor releases resources, than the copy will may become invalid.
To account for the possible problems with destruction, you can write your own deep copying function:
void deepCopy( my_class * destination, const my_class * source );
which you call instead of memcpy.
Note: please tell me if I went astray with anything here. I don't currently have a machine to test code on.
In the case of not having a default constructor, the use of load_construct_data and save_construct data actually permits the use of shared_ptr instances. In my view, there is no need to play around with memcpy and raw pointers. Thus you can just do something like the following
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/serialization/shared_ptr.hpp>
#include <boost/serialization/serialization.hpp>
#include <boost/serialization/string.hpp>
#include <memory>
class MyClass {
public:
explicit MyClass(std::string const &str) : m_str(str) {}
MyClass() = delete;
std::string str() const
{
return m_str;
}
private:
std::string m_str;
};
namespace boost { namespace serialization {
template<class Archive>
void serialize(Archive &ar,
MyClass const & myClass,
unsigned int const)
{
auto str = myClass.str();
ar & str;
}
template<class Archive>
void save_construct_data(Archive &ar,
MyClass const * myClass,
unsigned int const)
{
auto str = myClass->str();
ar << str;
}
template<class Archive>
void load_construct_data(Archive &ar,
MyClass * myClass,
unsigned int const)
{
std::string archived;
ar >> archived;
::new(myClass)MyClass(MyClass(archived));
}
}
}
int main(int argc, const char * argv[]) {
std::shared_ptr<MyClass> myClass(new MyClass("hello!"));
std::stringstream os;
::boost::archive::text_oarchive oa(os);
oa << myClass;
std::shared_ptr<MyClass> myClassB;
std::stringstream is;
is.str(os.str());
::boost::archive::text_iarchive ia(is);
ia >> myClassB;
return 0;
}

C++ - boost::any serialization

As far as I understand, there is no serialization (boost::serialization, actually) support for boost::any placeholder.
Does someone know if there is a way to serialize a custom boost::any entity?
The problem here is obvious: boost::any uses template-based placeholders to store objects and typeid to check if boost::any_cast is appropriate.
So, there is a custom abstract superclass placeholder and custom template-based derived classes, which are created the following way:
template <T> custom_placeholder : public placeholder {
virtual std::type_info type() const { return typeid(T); }
virtual ...
};
Obviously, this brings some troubles when even thinking about serializing this stuff. Maybe someone knows some trick to make such kind of serialization (and of course, proper deserialization)?
Thank you
If you want to stick with boost::any i am not sure but you can write your own "boost::any". I'm using this code for proxy methods to pass the parameters.
#include <iostream>
#include <boost\smart_ptr\scoped_ptr.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/serialization/access.hpp>
#include <boost/serialization/shared_ptr.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/serialization/export.hpp>
#include <sstream>
class my_placeholder
{
public:
virtual ~my_placeholder(){}
my_placeholder(){}
private:
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
// serialize base class information
//ar & boost::serialization::base_object<bus_stop>(*this);
//ar & m_placeholder;
}
};
template<typename T>
class my_derivedplaceholder:
public my_placeholder
{
public:
my_derivedplaceholder()
{
}
my_derivedplaceholder(T &value)
{
m_value=value;
}
T m_value;
private:
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
// serialize base class information
ar & boost::serialization::base_object<my_placeholder>(*this);
ar & m_value;
}
};
BOOST_CLASS_EXPORT_GUID(my_derivedplaceholder<int>, "p<int>");
class my_any
{
public:
my_any()
{
}
template<typename T>
my_any(const T &value)
{
m_placeholder.reset(new my_derivedplaceholder<T>(const_cast<T&>(value)));
}
template<typename T>
void operator=(const T &value)
{
m_placeholder.reset(new my_derivedplaceholder<T>(const_cast<T&>(value)));
}
protected:
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
// serialize base class information
//ar & boost::serialization::base_object<bus_stop>(*this);
ar & m_placeholder;
}
template<typename T>
friend T my_anycast(my_any &val);
boost::shared_ptr<my_placeholder> m_placeholder;
};
template<typename T>
T my_anycast(my_any &val)
{
boost::shared_ptr<my_derivedplaceholder<T>> concrete=boost::dynamic_pointer_cast<my_derivedplaceholder<T>>(val.m_placeholder);
if (concrete.get()==NULL)
throw std::invalid_argument("Not convertible");
return concrete->m_value;
}
void main()
{
my_any m=10;
int a=my_anycast<int>(m);
std::cout << a << std::endl;
std::stringstream ss,ss2;
boost::archive::text_oarchive oa(ss);
oa << m;
boost::archive::text_iarchive ia(ss);
my_any m2;
ia >> m2;
std::cout << my_anycast<int>(m2) << std::endl;
}
It is not possible at all, at least for arbitrary types. Note that maybe you could serialize using some tricky code (like finding the size of the elements contained in the any), but the any code relies on the compiler statically putting the any type_code and the proper types inside the placeholder. You surely cannot do that in deserialization in C++, as the type that you'd get from the deserialization is not known at compile time (as required by the newly formed boost::any).
The best solution is to build some kind of specialized any type for the exact types of elements you're going to serialize. Then, you can have special cases for the actual type of element being deserialized, but note that each element type serialization/deserialization has to be phisically written as static C++ code.
PD. Some others suggested using boost::variant as a representation of this specialized type holding the exact types you're going to serialize. You need a way of discerning the exact type on deserialization, though (maybe assigning identifiers to types in the variant).
Assuming you have to use boost::any and you cannot switch to variant, a map<type_info const*, string(*)(any)> based solution could get you done.
You have to initialize at runtime such a map with all the types you plan to use. Of course, you can use something along the lines of
template <typename T>
struct any_serializer
{
static string perform(any a)
{
T const& x = any_cast<T const&>(a);
stringstream out;
out << x;
return out.str();
}
};
and populate the map with addresses of any_serializer<T>::perform under the key &typeid(T). You can specialize the class any_serializer and use some (ugly) macros to populate the map.
More difficult is of course the deserialization. I haven't had a look at boost::lexical_cast for a while, perhaps it can provide some help. I am afraid that this is totally problem-dependant. However, you only need one function, which takes a string and returns one any. You may also want to prepend your output string with a custom type identifier.
There is no need to create new class. Try to use xany https://sourceforge.net/projects/extendableany/?source=directory
xany class allows to add new methods to any's existing functionality. By the way there is a example in documentation which does exactly what you want.

How to serialize shared/weak pointers?

I have a complex network of objects connected with QSharedPointers and QWeakPointers. Is there a simple way to save/load them with Boost.Serialization? So far I have this:
namespace boost {
namespace serialization {
template<class Archive, class T>
void save(Archive& ar, QSharedPointer<T> const& ptr, unsigned version) {
T* sharedPointer = ptr.data();
ar & BOOST_SERIALIZATION_NVP(sharedPointer);
}
template<class Archive, class T>
void load(Archive& ar, QSharedPointer<T>& ptr, unsigned version) {
T* sharedPointer = 0;
ar & BOOST_SERIALIZATION_NVP(sharedPointer);
ptr = QSharedPointer<T>(sharedPointer);
}
template<class Archive, class T>
void save(Archive& ar, QWeakPointer<T> const& ptr, unsigned version) {
T* weakPointer = ptr.toStrongRef().data();
ar & BOOST_SERIALIZATION_NVP(weakPointer);
}
template<class Archive, class T>
void load(Archive& ar, QWeakPointer<T>& ptr, unsigned version) {
T* weakPointer = 0;
ar & BOOST_SERIALIZATION_NVP(weakPointer);
ptr = QSharedPointer<T>(weakPointer);
}
}
}
This is not working because the shared pointers are always constructed from raw pointers so they all think the reference count is 1. It also immediately frees weak pointers.
With some effort I can convert the classes to use boost::shared_ptr and boost::weak_ptr. Will that do any good?
The question is what do you really want to achieve by serializing pointers? What is your expected output? Note that pointers point to a place in memory -- several may point to the same place in memory.
Serializing the address won't work. You can't just write down the exact memory address, because there's no way to guarantee that objects on the next run will be able to take the same space (another program may have already reserved that place).
Serializing the pointed object in each place where we have a pointer wont work:
This would be a lot more data that we'd need to serialize
If we had weak pointers creating a circular dependency we wouldn't be able to stop and retrieve that connection later.
There is no way to merge the same objects into one when deserializing
Now that you think about it, you can try the official answer of boost::serialization:
boost::serialization on shared_ptr
boost::serialization on shared_ptr 2