Boost:序列化:谁清理反序列化的数据? [英] Boost:Serialization: Who cleans up the deserialized data?

查看:399
本文介绍了Boost:序列化:谁清理反序列化的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将序列化并将对象的原始指针发送到另一个应用程序。



当反序列化时,我得到另一个原始指针。这意味着Boost :: Serialization在内部构造此指针后面的对象。我现在好奇谁拥有该对象的所有权,如果Boost将删除该对象,如果它不再需要。



一些代码,可能更好地显示什么问题是:

  void anyMethod()
{
std :: ifstream file(archiv.txt );
boost :: archive :: text_iarchive ia(file);
AnyClass * object;
ia>>目的;
//使用对象
}

//现在对象发生了什么?
//是否被删除,原因是它超出范围?
//我必须自己删除吗?

解决方案

关于指针序列化的//www.boost.org/doc/libs/1_44_0/libs/serialization/doc/serialization.html#pointeroperatorsrel =nofollow>文档是归档保留所有权:多次加载相同的指针对象导致只创建一个对象,从而复制原始指针配置。



此外,归档还提供了一个 delete_created_pointers 方法:删除通过加载指针创建的所有对象,这可以用于避免如果指针正在加载并且归档加载遇到异常时可能发生的内存泄漏。


I'm serializing and sending a raw-pointer of an object to another application.

When deserializing I'm getting another raw-pointer. This means that Boost::Serialization is constructing the object behind this pointer internally. I'm now curious who has ownership of the object and if Boost will delete that object, if it isn't needed anymore.

Some code which perhaps better shows what the question is:

void anyMethod()
{
    std::ifstream file("archiv.txt"); 
    boost::archive::text_iarchive ia(file); 
    AnyClass* object; 
    ia >> object; 
    //work with object
}

//Now what has happened to object?
//Is it deleted, cause it went out of scope?
//Do I have to delete it myself?

解决方案

My understanding of the documentation regarding pointers serialization is that ownership is kept by the archive : "Loading the same pointer object multiple times results in only one object being created, thereby replicating the original pointer configuration". This tends to indicate that the library handles the book keeping.

Furthermore, the archive provides a delete_created_pointers method : "Deletes all objects created by the loading of pointers. This can be used to avoid memory leaks that might otherwise occur if pointers are being loaded and the archive load encounters an exception".

这篇关于Boost:序列化:谁清理反序列化的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆