Check to see if an entity still exists at a handle

Blitz3D Forums/Blitz3D Programming/Check to see if an entity still exists at a handle

Craig H. Nisbet(Posted 2004) [#1]
I need to check if a handle for an entity is still valid(basically, if there is still a active entity associated with it). Is there a way to do this?


Gabriel(Posted 2004) [#2]
One way that springs to mind is to set all handles to zero when freeing them. For example, replace FreeEntity by adding this function to your code :

Function MyFreeEntity(ent)
FreeEntity(ent)
Return 0
End Function

And replace all FreeEntity(ent) calls with ent=FreeEntity(ent)


Tom(Posted 2004) [#3]
Yep, I'm creating a bunch of functions for this myself after a day of hunting down a memory leak that was caused by not freeing up a sound effect.

If you Free it, Zero it!


jfk EO-11110(Posted 2004) [#4]
Sometimes a code uses multiple instances of a handle, eg:

a=createcube()
b=a

a=myfreeentity(a)

; a is now zero, but b don't knows about it

Probably you can check if b is a valid entity when you use the entityclass$() command, but I didn't test it. Maybe it still thinks there is a mesh, I don't know. But it should be tested quickly.

if entityclass$(b)="Mesh" then print "b still seems to be a valid mesh"

EDIT: I just tested it, and there is a very funny result, or should I say pretty scary :/

When you FreeEntity a Mesh, EntityClass$() will return "Pivot" when used with the now invalid handle! So this could be an option, but only if you KNOW that it should return "Mesh". (Cause it returns "Mesh" when you didn't free the mesh).

BTW. if you use EntityClass$() with a value that has never been an entity (eg. zero), it will produce a MAV.

Probably EntityClass$() should be enhanced, allowing to check non-handles as well as handles that are no longer valid and then return an empty string.


Koriolis(Posted 2004) [#5]
Probably EntityClass$() should be enhanced, allowing to check non-handles as well as handles that are no longer valid and then return an empty string
The problem is that the handle is simply a pointer, which enables a fast, direct access. To do what you suggest it would have to scan the list of alive entities in order to know it's not a valid entity handle (though it could be enhanced with an associative container, but still).


jhocking(Posted 2004) [#6]
The problem of multiple handles pointing to an entity happens to me all the time. For example, in one recent project I had a type for all the tanks. The tanks store, as one field in the type, the handle of the object they are targetting. But this resulted in a MAV if multiple tanks target the same object and one of them destroys it.

An addition to EntityClass to handle invalid handles would be really handy for situations like this, but in the meantime note that attempting to access a type instance that has been deleted results in a 'null' response, not an error. So the workaround I use is to refer to the type instance that has the handle of the entity, not the entity's handle directly. When I free the entity I also delete the type instance. Thus, anything else attempting to refer to the type instance gets a 'null' response.