开发者

Boost serialization: How does pointer tracking work?

开发者 https://www.devze.com 2023-03-30 00:25 出处:网络
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

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:

Boost serialization: How does pointer tracking work?

Starting with A, using a stack to determine what we still need to visit:

  1. Visit A
  2. Push B, C
  3. Pop and Visit C
  4. Push 'D'
  5. Something else deletes B and replaces it with D
  6. Pop and visit 'D'
  7. Pop and visit what was labeled as B, but is now really D 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消