pointer question?

In my game server if I have a Player object and it is stored and pointed to with a shared_ptr in the player handler. Then that player is passed to an Npc object as a shared_ptr. Then it is stored in the Npc object as the current player the npc is attacking. What would happen if the playerHandler removed the player. Their is still an instance in the npc object. I could use weak_ptrs I suppose but if I didn't use them wouldn't it clog the memory This is just an example by the way. Would one shared_ptr and a bunch of weak_ptrs be acceptable in this instance? or is their a better way.
Please do not post more than once:
http://www.cplusplus.com/forum/general/142838/
It would crash if the value of the pointer was passed after it was destroyed, use references.

And I'd suggest removing the player object a synchronous, so it flags to remove it on the next convenient time. It'll save you a lot of trouble later on, especially with call stacks and threading.
Please reply to the original thread.
Krisando wrote:
It would crash if the value of the pointer was passed after it was destroyed, use references.
A std::weak_ptr knows whether the pointed-to object still exists. References do not. Your logic is reversed.
Krisando wrote:
And I'd suggest removing the player object a synchronous, so it flags to remove it on the next convenient time. It'll save you a lot of trouble later on, especially with call stacks and threading.
This is vague and prone to misinterpretation.
Last edited on
Topic archived. No new replies allowed.