From: Willem J. P. <wpa...@ma...> - 2008-07-24 21:19:06
|
On Thu, Jul 24, 2008 at 01:20:45PM -0700, Marzo Junior wrote: > I was playing with Valgrind recently and came across a slight > problem concerning the deletion of game objects. [snip] Thanks for bringing this up and starting a discussion! It's long past time we fix this. As another possible solution, the way Ultima 8 (and Pentagram) solved this is worth considering: Every game object (class Object) that is not cached out has a 16 bit ID. There's a master list that contains an ID -> Object* mapping, and a list of free object IDs that makes sure freed IDs are re-used roughly in the order they were freed. As few as possible Object*'s are stored in the engine, to make it impossible that there are still pointers to a deleted object around. The object ID acts as a weak_ptr, in a sense. The difference with the shared_ptr/weak_ptr system is that the object is not accessed through a shared pointer, but that there is only a single master copy. (Less flexible, but also a lower risk of leaks. It also limits our multi-threading options, but those are already fairly limited to begin with.) This also solves the serialization problem, since the object IDs remain the same after saving and loading. -Willem Jan |