How does pointer tracking work in boost serialization? I am using it to serialize messages between system components, and I have the impression that I ge开发者_Python百科t a lot of incorrectly shared objects. Is it possible that if I use shared pointers to the objects I want to serialize and a new object happens to have the same address as a previous object that has since been deleted this will incorrectly be captured as a shared refernce?
Imagine the following for objects for serializing:
Starting with A
, using a stack to determine what we still need to visit:
- Visit
A
- Push
B
,C
- Pop and Visit
C
- Push 'D'
- Something else deletes B and replaces it with D
- Pop and visit 'D'
- Pop and visit what was labeled as
B
, but is now reallyD
as well
Then you have a situation that will incorrectly be captured as a shared reference. If anything can change any of the pointers, or objects they point to between the start of the serialization and the end of it then you'll get an inconsistent state.
Short answer: yes, this happens.
The reason is (as one can conclude from the other answers and comments) that this is a slight abuse of what the serialization library is meant to do. Thus, if one uses boost serialization to log objects over the course of a day (e.g. not all objects are in memory at the start of the serialization), and objects happen to have the same address, they will be incorrectly shared. This happens on shared pointers, but also on regular pointers (to my understanding).
To fix this issue, either avoid pointers, or turn tracking off. If you turn tracking off, and still serialize shared pointers (which I currently do, although this might not be ideal), several hard coded checks in boost complain about tracking being turned off. To get around this, I hacked my local copy of the boost headers (and have to do so on every update and system used to compile) (this is pretty straight forward though, the compiler points to the right lines and they are clearly documented). If you have the choice to work without pointers, you spare yourself this trouble.
Other than that, the logging works brilliantly though, so don't be turned off from using boost serialization to this purpose (as some comments above suggest).
To log objects as they arrive, just use void Log(Msg* msg) {ar & NVP(msg);}
and to read them something along these lines:
Msg* Read() {
Msg* msg(0);
try {ar & NVP(msg);}
catch (boost::archive::archive_exception const&) { }
return msg;
}
You would call above Read function until the pointer returned is a NULL pointer, then you know the log is finished.
精彩评论