简体   繁体   English

Boost.Serialization是否释放加载指针期间创建的内存?

[英]Does Boost.Serialization Free the Memory Created during Loading Pointers?

To be specific, I'm referring to the demo in Boost.Serialization tutorial here . 具体而言,我指的是在Boost.Serialization教程演示这里

In the above demo, bus_top instances are stored as pointers in bus_route to showcase how serialization of pointers is achieved. 在上面的演示中, bus_top实例作为指针存储在bus_route以展示如何实现指针的序列化。 However, I observed an interesting behavior, that is, the destructor bus_stop::~bus_stop() is never invoked for the bus_stop objects created by restore_schedule() . 但是,我观察到一个有趣的行为,即从不为restore_schedule()创建的bus_stop对象调用析构函数bus_stop::~bus_stop() restore_schedule() I put a break point in bus_stop::~bus_stop() and it was triggered only by the delete statements at the end of main function. 我在bus_stop::~bus_stop()设置了一个断点,它只是由main函数末尾的delete语句触发。

During loading serialized data, objects to initialize pointers are allocated in pointer_iserializer defined in boost/archive/detail/iserializer.hpp , which uses a heap_allocator to allocate space. 在加载序列化数据期间,初始化指针的对象在boost/archive/detail/iserializer.hpp定义的pointer_iserializer中分配,该heap_allocator使用heap_allocator来分配空间。

They do have a destroy function to release allocated memory in boost/serialization/access.hpp , but seems never got invoked. 它们有一个destroy函数来释放boost/serialization/access.hpp分配内存,但似乎从来没有被调用过。

Is it correct that Boost.Serialization does not manage the pointers created during loading data? Boost.Serialization不管理加载数据期间创建的指针是否正确?

Conclusions: 结论:

  • The pointers are stored during loading and the objects they point to are created by new (ie boost::archive::detail::heap_allocator , boost::serialization::access::construct ) 指针在加载期间存储,它们指向的对象由new创建(即boost::archive::detail::heap_allocatorboost::serialization::access::construct
  • There is also a boost::serialization::access::destroy , but AFAIK, it's not got invoked in boost anywhere. 还有一个boost::serialization::access::destroy ,但据我所知,它没有得到在调用boost的任何地方。
  • It's the user's duty to free the objects allocated by boost::serialization , using delete (or boost::serialization::access::destroy if you like). 用户有责任释放boost::serialization分配的对象,使用delete (如果你愿意,可以使用boost::serialization::access::destroy )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM