Re: [boost] [serialization] Must the objects being serialized outlive the archive?

Robert,
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,
I don't really understand. Why does the tracking has something to do with the object's lifetime? Doesn't boost::serialization has all the information it needs (tracking info, data, etc.) when I call "oa << base_ptr"? Isn't it possible to just write out everything to the stream?
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 } ... }
As you can see from the code example, I want to be able to deserialize a list of these "commands" from the base pointer, because I actually have hundreds of these polymorphic commands and they need to each have a virtual "Execute()" method so that they know how to apply themselve to an "Actioner." (Instead of using a giant switch-case in the Actioner to identify each command) I figured it's better than using a magic number, opcode approach since boost::serialization already provides tracking. I was under the impression that if I want to deserialize using a base pointer, I must also serialize via the base pointer, is that correct? So I guess the below won't work with the original deserialization code?
{ 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 }
Best regards, Chris Yuen

Chris Yuen wrote:
Robert,
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,
I don't really understand. Why does the tracking has something to do with the object's lifetime? Doesn't boost::serialization has all the information it needs (tracking info, data, etc.) when I call "oa << base_ptr"? Isn't it possible to just write out everything to the stream?
IIUC, tracked objects have their addresses place in a map, so that subsequent serialized pointers of the same value are serialized as a reference to the original object at that address, rather than serializing another copy of the object. Your code makes it possible for base_ptr to have the same value on subsequent calls to Serializer::serialize, but for different types. Could be that seeing to different types with the same address is confusing things. I could be that if you only serialized Strings or Doubles, that you would not "seg fault", but you'd only see N of the first object serialized. HTH, Jeff

Chris Yuen wrote:
Robert,
As you can see from the code example, I want to be able to deserialize a list of these "commands" from the base pointer, because I actually have hundreds of these polymorphic commands and they need to each have a virtual "Execute()" method so that they know how to apply themselve to an "Actioner." (Instead of using a giant switch-case in the Actioner to identify each command) I figured it's better than using a magic number, opcode approach
it is better, and the serialization library supports and even encourages this. Just serialize and de-serialize through a base class pointer.
I was under the impression that if I want to deserialize using a base pointer, I must also serialize via the base pointer, is that correct?
correct.
So I guess the below won't work with the original deserialization code?
{ 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 }
This would work. What won't work is { boost::scoped_ptr<Strings> s; const Strings * s = new Strings("Hello", "World")); const * x = s; oa << s; delete s; oa << x; ... } Your making this way too hard, just look at the examples and tests to see that all you have to do is serialize though a base class pointer. When the (base class) pointer is deserialized, an object of the most derived class is created automagically. The serialization library takes care of keep track of the true (i.e. most derived type). Robert Ramey
participants (3)
-
Chris Yuen
-
Jeff Flinn
-
Robert Ramey