悬空指针...除了 std::weak_ptr 之外还有其他解决方案吗?
这比问题更令人沮丧,但仍然是。 几天前,我发现自己在计划中遇到了一个大问题。我会尝试并简要介绍遇到的情况。我的计划是每次发布某个键,假设空间时都会执行回调功能。它有一些参数,但是最重要的是指向某些对象的指针,在这种情况下,它是我游戏的实体对象。该实体作为唯一的_ptr存储在向量中,因此,当我在向量中删除该空间时,将为我清除内存。
足够好,对吧?好吧,问题在于,如果某些实体是从内存中删除的,并且在程序的下一个刻度中,我发布了空格键,则该回调函数是通过指向该已删除实体的指针执行的。我以前很愚蠢,我以为指针会神奇地转向nullptr,因此不继续回调,“我确实检查了指针在功能开始时是否为无效,并返回,如果确实为null,则返回。”
可以想象,即使删除了该实体,指针也不是无效的。它指出了一些没人关心的垃圾垃圾记忆。最终,我遇到了feal_ptr,将unique_ptr更改为向量中的共享_ptr,并将实体作为feam_ptr传递,而不是原始指针转换为我的回调。最后,我检查了feek_ptr是否具有“ lock()”功能的nullptr,这对我来说很整洁。如果删除内存,则该锁定()将返回null,如果没有,则将返回我功能中的共享_ptr。
您知道,我不想使用共享而不是唯一的原因是我不希望任何班级“拥有”我的实体。我希望我的实体一旦矢量脱离范围,或者已清除。然而,如果我们将指针作为feal_ptr通过指针,可以通过共享_ptr来实现单一所有权,因此没有其他班级会“拥有”我的实体。总之,std :: feek_ptr确实是一个很好的解决方案。
现在我的问题 - 是否有解决方案悬挂Pointers其他弱_ptr的解决方案? (是的,在删除向量之前,我已经将该唯一的_ptr设置为nullptr。但是它没有更改任何内容。悬挂的指针仍然指向某些垃圾记忆。)
This is more a disussion than a question, but still..
A few days ago I found myself having a big problem in my program. I'll try and be brief with the situation I encountered. My plan was to have a callback function being executed every time a certain key, let's say space, was RELEASED. It had a few parameters, however the most important one was a pointer to some object, in that case it was an entity object for my game. That entity was stored in a vector as an unique_ptr, hence the memory was cleared up for me when I deleted that space in the vector.
Good enough, right? Well, the problem was that if that certain entity was deleted from memory, and right in the next tick of the program I released the space key, that callback function was executed with a pointer to that deleted entity. Silly as I was, I thought that pointer was going to magically turn itself to nullptr, hence not continuing the flow of the callback, "I did check if the pointer was null in the beginning of the function, and return if it was indeed null."
As you can imagine, the pointer was not null even after that entity was deleted. It pointed to some garbage azz trash memory that no one cared about. Eventually, I came across weak_ptr, changed the unique_ptr to a shared_ptr in the vector, and passed the entity as a weak_ptr instead of raw pointer to my callback. Finally, I checked if the weak_ptr was nullptr with the "lock()" function, which worked pretty neat for me. If the memory was deleted, that lock() would return null, and if not, it would return a shared_ptr inside my function.
You see, the reason I didn't want to use shared instead of unique was that I didn't want any class to "own" my entity. I wanted my entity to be destroyed as soon as either the vector goes out of scope or it's cleared. Nevertheless, that single ownership can be achieved with shared_ptr IF we pass pointers as a weak_ptr, so no other class will "own" my entity. In conclusion, std::weak_ptr is acually a good solution.
Now my question - is there a solution for dangling pointers other the weak_ptr? (Yes, I had set that unique_ptr to nullptr right before deleting the vector. But it did not change anything. The dangling pointer still pointed to some garbage memory.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论