views:

281

answers:

1

I have a problem deserializing data serialized using Boost.Serialization.

I want a method like this DataObject* Transmitter::read(). DataObject is the parent class of multiple classes that can be send using Transmitter::write(DataObject& data). What I have at the moment looks like this, but it doesn't work.

DataObject* Transmitter::read()
{
    std::string dataString;

    // Data is read into the string here!
    // The data in dataString is correct, so this isn't the problem      

    std::istringstream inputStream(dataString);
    boost::archive::text_iarchive inputArchive(inputStream);
    DataObject* data;
    ia >> BOOST_SERIALIZATION_NVP(data);

    return data;
}

When I use it like that I get a boost::archive::archive_exception with "unregistered class". I looked at other examples that used serialization just like that, but it doesn't work inside the read() method of my class.

PS: On a side note I would like to use boost::archive::binary_iarchive. Can I use it like that in a stringstream or is this problematic because of zero-bytes?

+1  A: 

I think you should add this after DataObject class definition:

BOOST_CLASS_EXPORT_GUID(DataObject, "DataObject") 
// same as BOOST_CLASS_EXPORT(DataObject)

You should also export all class that derive from this, and must be included in the same file with Transmitter::read method implementation. Also all derived class should properly serialize themselves. To track bugs use xml serialization and print all data and check.

If it still don't work, you should check if you serialize the same type of object, DisplayObject& and DisplayObject* will make difference, but the error should be std::bad_alloc in that case.

Istringstream will work perfekt with boost::archive::binary_iarchive, cause underlaying stringbuf and strings doesn't use NULL terminating cstring.

lionbest
I currently use BOOST_CLASS_EXPORT(DataObject). I thought that was sufficient?
Koraktor
Just tried BOOST_CLASS_EXPORT_GUID, same problem.
Koraktor
I noticed that I serialized a reference while trying to deserialize to a pointer. But when I try to serialize a pointer I get the same error.I'm really stuck here. :(
Koraktor