开发者

Boost:Serialization: Who cleans up the deserialized data?

开发者 https://www.devze.com 2023-01-23 19:09 出处:网络
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 cons开发者_开发知识库truct

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 cons开发者_开发知识库tructing 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".


I don't understand why AnyClass* object; is legal in this context, IMO you have to have an object of AnyClass (Look at http://en.highscore.de/cpp/boost/serialization.html) then reuse it over and over. Now when this object ( not pointer ) goes out of scope, it will get deleted.


Sorry to answer this old question. I tried the demo, and found it has memory leak. The demo restores the archive to an object (which uses pointers internally) in a function. The archive is destructed when the function returned, and the object is returned. The allocated memory is not cleaned up by anyone. I modified the demo to run the restore function many times, then found the process took more and more memory. So if you need to clean up the allocated memory, manually delete the objects through the pointer, or call delete_created_pointers().

0

精彩评论

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