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

  • SEARCH
  • Home
  • 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 8915169
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T04:54:55+00:00 2026-06-15T04:54:55+00:00

I defined the following classes to be serialized: using namespace std; class MyElementObject {

  • 0

I defined the following classes to be serialized:

using namespace std;

class MyElementObject
{
    friend class boost::serialization::access;

    public:
        template<class Archive>
        void serialize(Archive & ar, const unsigned int version) { }
};

template<class T>
class MyRecursiveObject
{
    friend class boost::serialization::access;

    public:
        T element;
        std::vector<MyRecursiveObject<T> > children;

        template<class Archive>
        void serialize(Archive & ar, const unsigned int version)
        {
            ar & element;
            ar & children;
        }
};

I then run the following code:

int main()
{
    //MyRecursiveObject initialization
    MyRecursiveObject<MyElementObject> rec_object;    
    rec_object.children.push_back(MyRecursiveObject<MyElementObject>());
    rec_object.children[0].children.push_back(MyRecursiveObject<MyElementObject>());

    //create vector of pointers to MyRecursiveObject's elements
    vector<MyElementObject *> elt_ptrs;
    elt_ptrs.push_back(&rec_object.element);
    elt_ptrs.push_back(&rec_object.children[0].element);
    elt_ptrs.push_back(&rec_object.children[0].children[0].element);

    //serialize MyRecursiveObject and the vector of pointers
    {
        ofstream ofs("filename");
        boost::archive::text_oarchive oa(ofs);
        oa << rec_object;
        oa << elt_ptrs;
    }

    //create new MyRecursiveObject and vector of pointers for deserialization
    MyRecursiveObject<MyElementObject> rec_object_deserialized;    
    rec_object_deserialized.children.push_back(MyRecursiveObject<MyElementObject>());
    rec_object_deserialized.children[0].children.push_back(MyRecursiveObject<MyElementObject>());
    vector<MyElementObject *> elt_ptrs_deserialized;

    //deserialize
    {
        ifstream ifs("filename");
        boost::archive::text_iarchive ia(ifs);
        ia >> rec_object_deserialized;
        ia >> elt_ptrs_deserialized;
    }

    //compare deserialized pointers
    cout<<"elt_ptrs first level="<<elt_ptrs_deserialized[0]
    <<" expected="<<&rec_object_deserialized.element<<endl;

    cout<<"elt_ptrs second level="<<elt_ptrs_deserialized[1]
    <<" expected="<<&rec_object_deserialized.children[0].element<<endl;

    cout<<"elt_ptrs third level="<<elt_ptrs_deserialized[2]
    <<" expected="<<&rec_object_deserialized.children[0].children[0].element<<endl;

    return 0;
}

And I always get an output similar to the following one:

elt_ptrs first level=0x7fff57c787c0 expected=0x7fff57c787c0
elt_ptrs second level=0x18e7020 expected=0x18e7020
elt_ptrs third level=0xffff8000ab5564f0 expected=0x18e7450

As can be observed from the pointer values I manage to deserialize pointers that point to elements down to the second recursion level of MyRecursiveObject. As soon as I try to do it with pointers to the third level or even deeper the deserialization fails.

Am I using boost::serialization wrongly?

Please note that MyRecursiveObject is always successfully deserialized, no matter how many recursion levels it has. I encounter the problem only deserializing pointers to its elements.

Thank you in advance
Kean

  • 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-06-15T04:54:55+00:00Added an answer on June 15, 2026 at 4:54 am

    Let’s first examine what goes wrong. The default container deserializer basically works like this:

    size_t count;
    ar >> BOOST_SERIALIZATION_NVP(count); // get element count
    while( count-- > 0 ) {
        T temp; // <- and here's the problem!
        ar >> boost::serialization::make_nvp("item",temp);
        container.push_back(temp);
    }
    

    The container (in your case the vector<MyRecursiveObject<T>>) is filled using local variables. Unfortunately, their address is registered (object tracking) and referred to when you deserialize the vector<MyElementObject*>. In other words, your elt_ptrs_deserialized is pointing to local variables that are long gone.

    To fix that, serialize the vector manually without using local variables:

    template<class Archive>
    void serialize(Archive & ar, const unsigned int version)
    {
        ar & BOOST_SERIALIZATION_NVP(element);
        size_t count = children.size();        // 0 when loading, N when storing
        ar & BOOST_SERIALIZATION_NVP(count);   // load or store element count
        children.resize(count);                // should be a no-op when storing
        while( count-- > 0 )
            ar & boost::serialization::make_nvp("item",children[count]);
    }
    // You should split serialize() into load() and save() with
    // BOOST_SERIALIZATION_SPLIT_MEMBER() for a cleaner version
    

    Now, memory for the entire vector is allocated first and the elements are deserialized directly into it, therefore registering the correct memory address. This should yield the desired result:

    elt_ptrs 1st level=0x22fe90 expected=0x22fe90
    elt_ptrs 2nd level=0x6127d0 expected=0x6127d0
    elt_ptrs 3rd level=0x613d50 expected=0x613d50
    elt_ptrs 4th level=0x613c88 expected=0x613c88
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following classes defined to do validation: public class DefValidator : IValidate<IDef>
I have the following classes defined: public interface Thingy { ... } public class
I've defined the following classes public class AbstractManager<E> { public E save(E o) {
I have defined an Event class: Event and all the following classes inherit from
I have defined the following class using COM to use the IGroupPolicyObject using C#.NET:
I have the following classes with an implicit cast operator defined: class A {
Consider the following classes representing an Ordering system: Public Class OrderBase Public MustOverride Property
take two following classes: class Test1{ public: Test1()=default; Test1(char in1,char in2):char1(in1),char2(in2){} char char1; char
Scenario: I have the following defined classes. class Baseclass { }; class DerivedTypeA :
I'm writing some code where I defined the following base class. class Chorus{ public:

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.