Acceleration of serialization loading with an error is excluded

I have been trying to do this job for a long time.

There are 6 classes in my project that are serialized using the exact boost guide, by implementing template serialization.

These classes are: State, guState, Policy, Action, Param, Vec3D.

When I serialize and save them, it works great. I get a really text file with different numbers and lines.

No complaints, no warnings, no exceptions. The only case: if I try to serialize the class pointer element, the hole process will become a zombie. Therefore, I am not trying to do this and save work.

When I try to download, I get:

end of call after calling instance of 'boost :: archive :: archive_exception' what (): stream error

Now the interesting part is that I am serializing two boost :: ptr_vectors, one of which consists of status pointers and one that consists of policy pointers.

The state vector, I saved and loaded without problems. The policy vector I can save, but when I try to load, I get an exception.

In addition, after reading the tutorials to enhance, I got the impression that I didn't need anything else to download except the serialization function.

However, when I tried to load, the serialization boost would complain about not looking for a default constructor like State (), Policy (), etc. (I implement my own constructors in each class).

After reading this tutorial here, I applied a default constructor that does nothing, so serialization work will work. Indeed, it compiled, and I got the results mentioned above.

I tried to go down the difficult road seen in my old question here , where I tried to split and implement save_construct_data and load_construct_data, but I found it too inlaid, again I got the exact error as above.

Can someone please help me, explain how loading works, what is the deal with default constructors? Or at least give me a link that might be helpful. I went through the boost manuals and they say little about reconstruction.

Thanks.

Edit (several fragments added)

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 that inherit from the state also have their serialization 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, these are two main classes. Other classes are also serialized due to recovery dependencies (Action class, Param class, etc.)

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 two boost :: ptr_vectors that contain states and policies. States are saved and loaded without problems.

The problem occurs when loading policies. Saving them does not create a problem (but again, I could be wrong).

Test save / load without policies and with. The problem is with policy recovery.

Note the default constructors used only by serialization, without which the code would not compile.

EDIT # 2: after starting the 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 cannot tell where the leak is occurring, or if it is relevant to my serialization (I think it is).

+4
source share
1 answer

The problem is that you are serializing states and policies , and Policy also contains references to the same State instances. You can serialize classes that do not have such cross-references. Boost should throw a pointer-conflict-exception when writing to a file. In my tests, it depends on the recording order, if an exception was thrown or not, which is unsuccessful, because the download failed, even if the recording was successful.

A workaround would be to remove the line oa << states when saving and loading, and manually correct the pointers in the post-boot phase.

About the constructors: this is basically something like boost-api to make it template magic. However, when using version control, it is important to specify default values ​​for your member variables so that they do not remain uninitialized when downloading files with an older version number.

+3
source

All Articles