Boost logo

Boost Users :

From: vermaaso (vermaaso_at_[hidden])
Date: 2006-12-20 04:58:50


Hi Robert,

On Wed, Dec 13, 2006 at 08:29:40AM -0800, Robert Ramey wrote:
> I would be very interested in getting to the bottom of the problem. This
> would likely result in a small tweak which would make the library more
> portable. Of course I realise that this is not that easy. Note that I test
> here with gcc 3.3 under cywin. So I would be hopeful that its easily
> fixable. I don't know what version of boost your using, but it may already
> be fixed in a later version.

It looks like the problem is fixed (or at least, workaround-ed).

When I change

string serialize(const base * const b)

to

string serialize(base * const b)

the problem goes away (I don't get the segfault anymore).

I ran the regression tests on the suse (gcc 3.3.3) machine and the serialization tests all pass (except
for test_static_warning which gives a warn, but I guess that is what's supposed to happen).

I am going to use the workaround (remove the first 'const') and consider this thread closed.
Except when you want to investigate some more, I'll be glad to help, but then I need specific
instructions on what to test because this is getting a bit over my head ...

For completeness I included the original message once more at the bottom of this post.

Regards and thanks for your suggestions,
Oscar

-- Original Message:

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