如何有意要删除一个boost :: shared_ptr的? [英] How to intentionally delete a boost::shared_ptr?

查看:128
本文介绍了如何有意要删除一个boost :: shared_ptr的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有许多的boost :: shared_ptr的< MyClass的> 的对象,并在某些时候我故意要删除他们中的一些以释放一些内存。 (我知道在这一点上,我永远不会需要指向的 MyClass的的对象了。)我怎样才能做到这一点?

I have many boost::shared_ptr<MyClass> objects, and at some point I intentionally want to delete some of them to free some memory. (I know at that point that I will never need the pointed-to MyClass objects anymore.) How can I do that?

我想你不能只是叫删除()与原始指针,我与 get()方法获得

I guess you can't just call delete() with the raw pointer that I get with get().

我见过一个函数 get_deleter(shared_ptr的&LT; T&GT;常量和放大器; P)的boost :: shared_ptr的,但我不知道如何使用它,而且它说的实验的就在旁边。 (我想我有提升1.38)。

I've seen a function get_deleter(shared_ptr<T> const & p) in boost::shared_ptr, but I'm not sure how to use it, and also it says experimental right next to it. (I think I have Boost 1.38.)

也许只是分配一个新的空的boost :: shared_ptr的的变量?这应该丢掉旧值并将其删除。

Maybe just assign a new empty boost::shared_ptr to the variable? That should throw away the old value and delete it.

推荐答案

您只是做

ptr.reset();

查看 shared_ptr的手动。它相当于

shared_ptr<T>().swap(ptr)

您拨打重置上不应引用对象了每一个智能指针。最近一次的重置(或导致引用计数下降到零,实际上任何其他操作)将导致对象自动使用的删除被free'ed。

You call reset on every smart pointer that should not reference the object anymore. The last such reset (or any other action that causes the reference count drop to zero, actually) will cause the object to be free'ed using the deleter automatically.

也许你感兴趣的智能指针编程技术的。它有大约延迟释放的条目。

Maybe you are interested in the Smart Pointer Programming Techniques. It has an entry about delayed deallocation.

这篇关于如何有意要删除一个boost :: shared_ptr的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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