For very historical reasons, our handling of referring to member objects of games (via the GameObject and GameObjectPtr system) conflates two logically distinct considerations:
- The validity of an object (that is, whether it continues to refer to a legitimate part of the game, or has been deleted at some point by some operation);
- The reference counting of pointers to an object (to ensure memory gets deallocated correctly).
This leads to object deletion at potentially surprising times, because in GameObject there is a place where delete this is called - which is not ideal, even if it is technically legal. Without being exceptionally careful one can easily enough create a situation where an operation is attempted on an object that was deleted as part of the Invalidate() call on it.
- Use
std::shared_ptr and std::weak_ptr as appropriate to handle memory management of game component objects
- Create a new variation on
GameObjectPtr which stores a std::shared_ptr to the object, and checks for validity, but which does not have any responsibility for reference counting.
For very historical reasons, our handling of referring to member objects of games (via the
GameObjectandGameObjectPtrsystem) conflates two logically distinct considerations:This leads to object deletion at potentially surprising times, because in
GameObjectthere is a place wheredelete thisis called - which is not ideal, even if it is technically legal. Without being exceptionally careful one can easily enough create a situation where an operation is attempted on an object that was deleted as part of theInvalidate()call on it.std::shared_ptrandstd::weak_ptras appropriate to handle memory management of game component objectsGameObjectPtrwhich stores astd::shared_ptrto the object, and checks for validity, but which does not have any responsibility for reference counting.