Problem with Serialization: invalid signature

Hi all I have some trouble with the serialization of objects in C++ using boost. Since there was some strange behaviour in the code, I tried to serialize and deserialize a sample object isolated trying two versions: |deSerialize(Packet p1, Packet p2){ // Version 1 using stringstreams std::ostringstream *serialized = new std::ostringstream[2];|| ||serialized[0]=std::ostringstream(std::ios_base::binary);|| ||serialized[1]=std::ostringstream(std::ios_base::binary);|| || || boost::archive::text_oarchive oa(serialized[0]);|| || oa << p1;|| || || boost::archive::text_oarchive oa2(serialized[1]);|| || oa2 << p2;|| || || std::string s1, s2;|| || || s1 = serialized[0].str();|| || s2 = serialized[1].str();|| || || //deserialize CCNDataPkt newPacket; std::istringstream inputStream(s2, std::ios_base::binary); boost::archive::text_iarchive ia(inputStream); | | ia >> newPacket;| // throws boost::archive::archive_exception: invalid signature. } and |deSerialize(Packet p1, Packet p2){ // Version 2 using ofstreams ||std::ofstream of("deleteme",std::ios_base::binary);|| || boost::archive::binary_oarchive oa(of);|| || oa << p1;|| || || std::ifstream ifstr("deleteme", std::ios_base::binary);|| || CCNDataPkt newp;|| || boost::archive::binary_iarchive ia(ifstr);|| | | ia >> newp;| // boost::archive::archive_exception: invalid signature. } However, both variants returned an invalid signature exception even though I haven't changed anything on the serialized data. Does anyone know what is wrong in either of the examples above? The methods for serializing the custom object 'Packet' are implemented according to the boost serialization tutorial (non-intrusive version splitted) Any hints are very much appreciated - TIA! Cheers, Alex

deSerialize(Packet p1, Packet p2){ // Version 1 using stringstreams
<...>
boost::archive::text_oarchive oa(serialized[0]); oa << p1;
<...>
//deserialize CCNDataPkt newPacket;
<...>
ia >> newPacket; // throws boost::archive::archive_exception: invalid signature.
It looks like you serialize one type and deserialize another one, don't you?

Thanks for pointing that out - actually, it was just an oversight. I first pasted the code (where the type is CCNDataPkt) and then thought it might of no interest what kind of packet the type is - and forgot to adapt it in the deserialization part. In fact, types _are_ the same - I'm sorry for the confusion! On 04.04.2013 14:11, Igor R wrote:

Zitat von Alexander Striffeler <a.striffeler@students.unibe.ch>:
However, both variants returned an invalid signature exception even though I haven't changed anything on the serialized data.
I cant think of a good reason why this would happen with your code. invalid_signature is only thrown if the archive signature isnt found at the beginning of the archive. so you should receive this exception before deserializing any objects. could be because the archive was created with the no_header flag, or missing ios::binary, or using a binary archive that was created on a different architecture (sent data through network?), ..., but none of that is in your code example. if you're not using old archives created by other code: no idea. however, since you seem to also serialize individual objects, you might be interested in this archive: http://pastebin.com/PTdRE8tR it serializes objects directly to a std::vector<char> or any other char container, circumventing streams. The Serialization library is only invoked on demand, if your objects are track_never and object_serializable no Serialization archive is constructed. constructing a binary_oarchive for each object can be quite expensive (codecvt construction, pimpl construction, archive internals for pointer tracking, ...)

On 2013-04-04 16:18, Alexander Striffeler wrote:
(sent data through network?), ..., That's what I'm actually up to (omnet) in the end, but I thought of just
You may also want to look at http://protoc.sourceforge.net/ which contains archives for various encodings (e.g. JSON and MessagePack). Protoc is still work-in-progress though.
participants (4)
-
Alexander Striffeler
-
Bjorn Reese
-
Igor R
-
Stefan Strasser