[Boost-users] Problem with Serialization: invalid signature

470 views
Skip to first unread message

Alexander Striffeler

unread,
Apr 4, 2013, 4:59:10 AM4/4/13
to boost...@lists.boost.org
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

Igor R

unread,
Apr 4, 2013, 8:11:19 AM4/4/13
to boost...@lists.boost.org
> 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?
_______________________________________________
Boost-users mailing list
Boost...@lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/boost-users

Alexander Striffeler

unread,
Apr 4, 2013, 8:20:57 AM4/4/13
to boost...@lists.boost.org
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!

Stefan Strasser

unread,
Apr 4, 2013, 9:39:43 AM4/4/13
to boost...@lists.boost.org
Zitat von Alexander Striffeler <a.stri...@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, ...)

Alexander Striffeler

unread,
Apr 4, 2013, 10:18:12 AM4/4/13
to boost...@lists.boost.org
Hi Stefan

Thank you very much for your answer!

> (sent data through network?), ...,
That's what I'm actually up to (omnet) in the end, but I thought of just writing some sample code to ban such side effects. For the problem described in my original post, I've only used the lines stated there (no other archives).

I'll just have a closer look at your archive code, thanks a lot!

Bjorn Reese

unread,
Apr 4, 2013, 11:58:40 AM4/4/13
to boost...@lists.boost.org
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.
Reply all
Reply to author
Forward
0 new messages