|
Boost : |
Subject: Re: [boost] [serialization] Must the objects being serialized outlivethe archive?
From: Robert Ramey (ramey_at_[hidden])
Date: 2010-09-15 12:35:55
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
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk