Boost logo

Boost Users :

From: vermaaso (vermaaso_at_[hidden])
Date: 2006-12-12 09:55:52


Hello list,

currently I am trying to get serialization to work.
It worked well when I was using boost 1.32, but since I upgraded to 1.33 I'm having some problems.

In short;
I have an abstract base class and a derived class. I try to serialize/deserialize the derived class
through a pointer to the base class.
This worked well when I was using 1.32.

When I upgraded to 1.33 I got the static assert which trapped the 'saving non const error' (I hope this makes sense ..).
So I fixed it by following instructions from other posts dealing with the same problem
(basically just by changing 'base *b' to 'const base * const b').

The fix resolved the compile error, only now I'm getting a segfault at runtime.
The segfault happens during deserialization (when trying to 'reconstruct' the original object).

The funny thing is, I've tested it on a system (suse enterprise system 9, gcc 3.3.3) and it chrashes.
On another system (gcc version 4.0.3 (Ubuntu 4.0.3-1ubuntu5)) it works fine.

I reproduced the problem in a very small program located at the end of this post. Maybe someone
can have a look at the code and tell me whether there are obvious problems with my code or not.
If I know for sure that my code should run correctly, I'll just move on to an other linux distro ..

Thanks in advance,
Oscar

#include<boost/archive/text_oarchive.hpp>
#include<boost/archive/text_iarchive.hpp>
#include<boost/serialization/base_object.hpp>
#include<boost/serialization/export.hpp>
#include<iostream>
#include<sstream>

using namespace std;

class base {
public:
  virtual void do_it() const = 0;
private:
  friend class boost::serialization::access;
  template<class Archive>
  void serialize(Archive & ar, const unsigned int){
    cout << "serializing base" << endl;
  }
};

class derived: public base {
public:

  derived(int field1, string field2){
    _field1 = field1;
    _field2 = field2;
  }
  virtual void do_it() const {
    cout << "doing it with " << _field1 << " and " << _field2 << endl;
  }
private:
  friend class boost::serialization::access;
  derived(){};
  virtual ~derived(){};
  template<class Archive>
  void serialize(Archive & ar, const unsigned int){
    cout << "derived being serialized" << endl;
    ar & boost::serialization::base_object<base>(*this);
    ar & _field1;
    ar & _field2;
  }
  int _field1;
  string _field2;
};

BOOST_IS_ABSTRACT(base);
BOOST_CLASS_EXPORT(derived);

string serialize(const base * const b){
  stringstream str;
  boost::archive::text_oarchive oa(str);
  oa << b;
  return str.str();
}

base *deserialize(string s){
  stringstream str(s);
  base *b;
  boost::archive::text_iarchive ia(str);
  ia >> b;
  return b;
}

int main(){
  base *d1 = new derived(52,"a string");
  d1->do_it();

  string serialized = serialize(d1);

  base *d2 = deserialize(serialized);
  d2->do_it();

  return 0;
}


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net