title: Boost serializing loading fails with exception thrown
body: <p>I have been trying to make this work for a long time now.</p>
  
  <p>In my project there are 6 classes that are being serialized using the exact tutorial from boost, by implementing the template function serialize.</p>
  
  <p>Those classes are: State, guState, Policy, Action, Param, Vec3D.</p>
  
  <p>When I serialize and save them, it works fine. I get indeed a text file, with various numbers and strings in it.</p>
  
  <p>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.</p>
  
  <p>When however I try loading, I get:</p>
  
  <blockquote>
    <p>terminate called after throwing an instance of
    'boost::archive::archive_exception' what():  stream error</p>
  </blockquote>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>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 ).</p>
  
  <p>After reading <a href="http://www.informit.com/articles/article.aspx?p=398702&amp;rll=1" rel="nofollow noreferrer">this tutorial here</a> 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.</p>
  
  <p>I have tried going down a much complicated road seen in <a href="https://stackoverflow.com/questions/6734814/boostserialization-reconstruction-loading">my old question here</a> 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.</p>
  
  <p>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.</p>
  
  <p>Thank you.</p>
  
  <p>Edit (Added a few snippets)</p>
  
  <pre><code>class State
  {
      friend class boost::serialization::access;
      template&lt;class Archive&gt;
      void serialize(Archive &amp; ar, const unsigned int version);
  
    protected:
      std::map&lt;std::string,float&gt; positions;
      float reward;
      std::size_t hash_value;
      bool exists(const Action* A);
      bool operator== (State const&amp; 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&lt;Action&gt; actions;
      State(ACTION_MODE &amp;m);
      ~State();
      bool operator== (State const* S);
      bool operator&lt; (State const* S) const ;
      const float&amp; getR() const;
      bool addAction(Action *A);
      Action* findAction(const Action *A);
      boost::ptr_vector&lt;Action&gt;&amp; getAllActions();
      void printState();
      virtual bool isTerm();
  };
  
  template &lt;class Archive&gt;
  void State::serialize(Archive&amp; ar, const unsigned int version)
  {
    ar &amp; BOOST_SERIALIZATION_NVP(positions);
    ar &amp; BOOST_SERIALIZATION_NVP(gps);
    ar &amp; BOOST_SERIALIZATION_NVP(current_position);
    ar &amp; BOOST_SERIALIZATION_NVP(reward);
    ar &amp; BOOST_SERIALIZATION_NVP(hash_value);
    ar &amp; BOOST_SERIALIZATION_NVP(actions);
    ar &amp; BOOST_SERIALIZATION_NVP(my_policy);
  }
  </code></pre>
  
  <p>Other Classes inheriting from State, have also their serialize functions, using:</p>
  
  <pre><code>  ar &amp; boost::serialization::base_object&lt;State&gt;(*this);
  </code></pre>
  
  <p>Class Policy:</p>
  
  <pre><code>class Policy
  { 
      friend class boost::serialization::access;
      template&lt;class Archive&gt;
      void serialize(Archive &amp; 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 &lt;class Archive&gt;
  void Policy::serialize(Archive&amp; ar, const unsigned int version)
  {
    ar &amp; BOOST_SERIALIZATION_NVP(action);
    ar &amp; BOOST_SERIALIZATION_NVP(state);
    ar &amp; BOOST_SERIALIZATION_NVP(QValue);
    ar &amp; BOOST_SERIALIZATION_NVP(r);
  }
  </code></pre>
  
  <p>As you can see those are the two main classes, Other classes are also serialized because of reconstruction dependencies ( class Action, class Param, etc )</p>
  
  <p>The master Class:</p>
  
  <pre><code>template &lt;class S, class P&gt; class Task
  { 
    protected:
      ...
      //! Container of states of type S (template parameter)
      boost::ptr_vector&lt;S&gt; states;
      //! Container of policies of type P (template parameter)
      boost::ptr_vector&lt;P&gt; policies;
      ...
  
    public:
  
      Task(Agent &amp;a, ACTION_MODE &amp;m);
      ...
      void save_to_file();
      void load_from_file(std::string filename); 
  };
  
  template &lt;class S, class P&gt; 
  void Task&lt;S,P&gt;::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 &lt;&lt; states;
    oa &lt;&lt; policies;
    ofs.close();
  }
  
  template &lt;class S, class P&gt; 
  void Task&lt;S,P&gt;::load_from_file(std::string filename)
  {
    char *file = (char*)output.c_str();
    std::cout &lt;&lt; file &lt;&lt; std::endl;
    std::ifstream ifs(file);
    boost::archive::text_iarchive ia(ifs);
    ia &gt;&gt; states;
    ia &gt;&gt; policies;
    ifs.close();
  }
  </code></pre>
  
  <p>Effectively contains the two boost::ptr_vectors which hold the States and Policies.
  States are saved and loaded without a problem.</p>
  
  <p>The problem arises when loading policies. Saving them does not seem to create a problem (but then again I might be wrong).</p>
  
  <p>Having tested save/load without policies, and with, The issue appears to be with policy reconstruction.</p>
  
  <p>Note the default constructors used only by serialization, without which the code will not compile.</p>
  
  <p>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).</p>