[serialization] Must the objects being serialized outlive the archive?

Hi, I am using boost::serialization along with the eos::portable_archive (on Boost Vault). All the data that I serialize are polymorphic types, i.e. pointers. By experiment and days of frustration, I found out that the objects being serialized must "outlive" the archive or the serialization process -- otherwise I'd get segfault and corrupt data. I hope that was clear. To demonstrate this, I wrote a small example at http://boost.codepad.org/mqX5dUf6 (I'm a noob and I don't know the tradition to paste code -- should I just paste it in the message body or something?) The program will crash when you run it. However, if you comment out both the "delete" statements (leading to memory leaks, but at least the objects will outlive the archives), then everything works fine. Is there a way so that I can "flush" the serialization process so that it gets written immediately? The way that it is now, I must keep in memory all the things that I want to serialize. Thanks! Chris Yuen

Zitat von Chris Yuen <kizzx2+boost@gmail.com>:
Hi,
I am using boost::serialization along with the eos::portable_archive (on Boost Vault). All the data that I serialize are polymorphic types, i.e. pointers. By experiment and days of frustration, I found out that the objects being serialized must "outlive" the archive or the serialization process -- otherwise I'd get segfault and corrupt data.
I hope that was clear. To demonstrate this, I wrote a small example at http://boost.codepad.org/mqX5dUf6 (I'm a noob and I don't know the tradition to paste code -- should I just paste it in the message body or something?) The program will crash when you run it. However, if you comment out both the "delete" statements (leading to memory leaks, but at least the objects will outlive the archives), then everything works fine.
objects don`t necessarily have to outlive the destruction of the archive, but you`re serializing multiple _tracked_ objects that - I guess - have the same address because you "new" and "delete" them consecutively. read the chapter about "object tracking" in the boost.serialization doc, and then if you really need to serialize "temporaries" make sure that they`re not tracked.

Chris Yuen wrote:
Hi,
I am using boost::serialization along with the eos::portable_archive (on Boost Vault). All the data that I serialize are polymorphic types, i.e. pointers. By experiment and days of frustration, I found out that the objects being serialized must "outlive" the archive or the serialization process -- otherwise I'd get segfault and corrupt data.
I hope that was clear. To demonstrate this, I wrote a small example at http://boost.codepad.org/mqX5dUf6 (I'm a noob and I don't know the tradition to paste code -- should I just paste it in the message body or something?) The program will crash when you run it. However, if you comment out both the "delete" statements (leading to memory leaks, but at least the objects will outlive the archives), then everything works fine.
Is there a way so that I can "flush" the serialization process so that it gets written immediately? The way that it is now, I must keep in memory all the things that I want to serialize.
It is a fundamental concept that saving data to an archive should not alter the data. In many cases this is detected at compile time and a warning is issued. As noted in another post, you might snooker the system by suppressing tracking, and it might or might not work, but the library is coded on the assumption that no one is going to do this so you would then be using a component in a way that is not guarenteed to work. Should your program fail you'd likely be faced with tracing execution 100 levels deep to verify whether violation of this assumption has created a problem. (Sounds like you've been there already). So this is not a good idea even it can be made to work. But I'm intrigued why you want to do this? Why don't you just use { std::ofstream stream(kFilename, std::ios::binary); boost::archive::text_oarchive oa(stream); { const Strings s("Hello", "World")); oa << s } ... } Robert Ramey

Robert Ramey wrote:
But I'm intrigued why you want to do this? Why don't you just use
{ std::ofstream stream(kFilename, std::ios::binary); boost::archive::text_oarchive oa(stream);
{ const Strings s("Hello", "World")); oa << s } ... }
or maybe { std::ofstream stream(kFilename, std::ios::binary); boost::archive::text_oarchive oa(stream); { boost::scoped_ptr<Strings> s; const Strings * s = new Strings("Hello", "World")); oa << s; .... // same for all the other stuff // s deleted as it goes out of scope } ... }
Robert Ramey
_______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
participants (3)
-
Chris Yuen
-
Robert Ramey
-
Stefan Strasser