Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6530049
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T09:42:32+00:00 2026-05-25T09:42:32+00:00

I have been trying to make this work for a long time now. In

  • 0

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).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T09:42:33+00:00Added an answer on May 25, 2026 at 9:42 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been trying to make this to be a little jQuery plugin that
Using MVC 2 I have been trying to make this record store project. Creating
Have been struggling all day trying to make this simple example work using socket.io.
I am trying to make this work but have been unlucky it is giving
I have been trying for hours to make this work but I am not
I have been trying to make a case for using Python at my work.
I have been trying to test my application to make sure that all the
I have been trying to make a StringTable class that holds a simple unordered_map<string,
Hi I have been trying to work out the best way to do this
I'm trying to make a WCF service that will work with JSON-P (long-story short,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.