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 get 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?
How does pointer tracking work in boost serialization? I am using it to serialize
Share
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:You would call above Read function until the pointer returned is a NULL pointer, then you know the log is finished.