简单高效的弱指针,在释放目标内存时将其设置为NULL [英] Simple, efficient weak pointer that is set to NULL when target memory is deallocated

查看:180
本文介绍了简单高效的弱指针,在释放目标内存时将其设置为NULL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一个简单,有效的弱/受保护指针??我需要指向同一对象的多个指针,这些指针在删除对象时都自动设置为NULL.总是有一个主"指针总是用于删除对象,但是也可以有多个其他指针引用同一对象.

Is there a simple, efficient weak/guarded pointer? I need multiple pointers to the same object that are all automatically set to NULL when the object is deleted. There is one "master" pointer that is always used to delete the object, but there can be several other pointers that reference the same object.

以下是一些与我的需求不完全匹配的解决方案:

  • QPointer :我没有开发QT应用程序;我不希望包含QObject的这个库/源代码.
  • boost :: weak_ptr :访问已释放对象时,将引发异常.对于我的情况来说太贵了:测试弱指针应该是正常的;我打算在弱指针不再有效时进行一些手动清理. 更新:weak_ptr可以在不引发异常的情况下进行测试
  • 低开销的弱指针:这与我要寻找的非常接近,除了我不喜欢只要您不在同一位置分配2 ** sizeof(int)次,就可以保证该方案可以正常工作."
  • QPointer: I am not developing a QT app; I do not wish to include this libary/derive from QObject.
  • boost::weak_ptr: an exception is thrown when accessing a deallocated object. Too expensive for my situation: it should be normal to test a weak pointer; I plan to do some manual clean-up when a weak pointer is no longer valid. update:weak_ptr can be tested without throwing exceptions
  • Low-Overhead Weak Pointers: This is very close to what I am looking for, except I don't like the fact "This scheme is only guaranteed to work as long as you don’t allocate 2**sizeof(int) times in the same location."

为什么我需要这些弱点/受保护的指针: 我有一个带有游戏对象列表的游戏.一些对象依赖于其他对象,例如与游戏实体关联的调试/统计对象.调试/状态对象显示有关游戏实体的有用信息,但仅在游戏实体存在时才有意义.因此,如果游戏实体被删除,则debug/stats对象应意识到这一点并自行删除. (另一个想法是追踪导弹:它可能会寻找新的目标,而不是自行删除.)

Why I need these weak/guarded pointers: I have a game with a list of game objects. Some objects are dependent on others, for example a debug/stats object that is associated with a game entity. The debug/status object displays useful info about the game entity, but it only makes sense while the game entity exists. So if the game entity is deleted, the debug/stats object should realize this and delete itself. (Another idea is a tracking missile: instead of deleting itself, it may search for a new target.)

我希望将调试/统计逻辑与游戏实体分开.游戏实体不必知道已附加调试/统计信息对象.尽管我希望为弱指针/守卫指针提供答案,但我也欢迎采用不同的方式来完成我的特定任务.我认为我可能必须实施游戏对象管理器跟踪对象的生存期,并使用句柄而不是指向内存地址的原始指针.

I wish to keep the debug/stats logic separate from the game entity. The game entity should not have to know a debug/stats object is attached to it. While I'd prefer an answer for weak/guarded pointers, I also welcome different ways to approach my specific task. I am thinking I may have to implement a game object manager that tracks object lifetimes and uses handles instead of raw pointers to memory addresses.

我正在用C ++开发.

I am developing in C++.

推荐答案

您可以使用boost::weak_ptrlock()成员来测试(然后使用)weak_ptr的值,而无需处理异常.

You can use the lock() member of boost::weak_ptr to be able to test (then use) the value of the weak_ptr without dealing with exceptions.

这篇关于简单高效的弱指针,在释放目标内存时将其设置为NULL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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