boost shared_ptr 获取所有者计数
我正在使用 boost::shared_ptr 来存储指向纹理的指针。我正在根据需要加载新纹理,并使用shared_ptr 在程序之间共享它们。如果我的应用程序使用了太多内存,我想删除未使用的纹理以清除内存。有没有办法可以确定有多少对象可以通过 shared_ptr 访问纹理?
I'm using boost::shared_ptr to store a pointer to texture. I'm loading new textures as i need and share them among the program using shared_ptr. If my app is using too much memory i want to remove unused textures to clear memory. Is there a way I can determine how many objects are having access to the texture via shared_ptr ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果未使用,则
shared_ptr
将自动释放它。这就是shared_ptr
的要点。如果您将shared_ptr
保存到纹理而不实际使用它,那么您就违反了shared_ptr
的约定,并且不应该使用它。If it's unused then the
shared_ptr
will free it automatically. That's the point ofshared_ptr
. If you are holding ashared_ptr
to a texture without actually using it, then you're violating the contract ofshared_ptr
and should not be using it.您可以使用
shared_ptr::use_count()< /code>
,但在这样做之前请阅读文档!
You can use
shared_ptr::use_count()
, but read the documentation before doing so!有
use_count()
< /a>,但请注意,正如文档所述,它不一定太高效。There is
use_count()
, however note that as the documentation says, it's not necessarily too efficient.shared_ptr
类具有成员函数use_count()
和unique()
,可让您访问其使用计数。不过,这些信息对您有何用处则是另一个问题。
The
shared_ptr
class has member functionsuse_count()
andunique()
to give you access to its usage count.It's a different question how that information will be useful to you, though.