how to call ptr_for_handle without "crash"

Posted By: Oxy

how to call ptr_for_handle without "crash" - 03/29/11 21:52

When I create a handle of an object,
remove the object (ent_remove)
and retreive it (ENITY) later with
ptr_for_handle,
the engine will call an error on warn_level > 1

But checking if the handle is NULL before retreiving the pointer does not work.

How do I know if a handle is empty, or an invalid (removed) object
BEFORE calling ptr_for_handle?

Example:

Code:
for(i=0;i<counter;i++)
{
	if(entities[i]!=0)  //DOES NOT RESPOND TO NULL
	{
		//will cause an error thrown
		testEnt=ptr_for_handle(entities[i]);
		
		//well here I can check if its NULL, but thats too late

		if( checkEnt!=NULL)
		{
			...

		}
	}
}


....

ent_remove(entityToKill); //killing an entity at some time


Posted By: Oxy

Re: how to call ptr_for_handle without "crash" - 03/29/11 22:31

any idea how to check a handle, if its valid?
Posted By: SchokoKeks

Re: how to call ptr_for_handle without "crash" - 03/29/11 22:40

As far as i remember you are doing it the right way. entites[i] is a var array and the entites are saved with handle(ENTITY*) there, right?

when warn_level is set > 1, a warning is issued, but you can ignore it. only solution is to set warn_level to 1.
Posted By: Oxy

Re: how to call ptr_for_handle without "crash" - 03/29/11 22:45

But why is there no way to check if a handle is null,
before having the engine issue a warning.

There must a a "right" way to handle this.
Or did the engine creators forget about this
(rather standard) case?
some command like ptr_is_valid(handleid);


by lowering the warn_level I would miss other errors,
that could come up.
Posted By: Superku

Re: how to call ptr_for_handle without "crash" - 03/29/11 22:50

entities[entityToKill.id] = 0;
ent_remove(entityToKill);

Is that an acceptable workaround for you?
Posted By: Oxy

Re: how to call ptr_for_handle without "crash" - 03/29/11 22:59

No, after the entity is removed, its pointer is null/invalid (and thus all skills are unreadalbe),
and I operate on a list of handles to entities, read out
by other codeparts.

I guess its simply an engine flaw, to miss
a robust way to check for valid handles, without a
warning thrown.

When I develop something, I always like to have errors thrown and not swallowed,
but not if there is no correct way to handle it.



But thanks for your help
Posted By: Superku

Re: how to call ptr_for_handle without "crash" - 03/30/11 00:01

Quote:
No, after the entity is removed, its pointer is null/invalid (and thus all skills are unreadalbe),
and I operate on a list of handles to entities, read out
by other codeparts.

I don't understand. When you remove the entity, simply set the corresponding handle to zero, i.e. before the ptr_/ent_remove instruction.
Then, in your list of handles, the handle will be zero and thus can be ignored, so you don't have to call ptr_for_handle anymore.
Posted By: Oxy

Re: how to call ptr_for_handle without "crash" - 03/30/11 00:10

I know what you mean.
But there is not only a single array for each handle.

There are many possible locations, and references where it
could have been stored before.
And I cant resolve all of them.
So this would be only a workaround for this specific case.
Posted By: Superku

Re: how to call ptr_for_handle without "crash" - 03/30/11 00:40

Ok, then you could/ should contact the developers so ptr_for_handle returns NULL when the handle is invalid.
Posted By: Oxy

Re: how to call ptr_for_handle without "crash" - 03/30/11 02:07

Right now I have the problem that a removed entity
is not recognized as NULL, but
is like a "ZERO" entity, with positiondata at 0,0,0 for example.

This engine really has some flaws.


if(myTarget!=NULL)
{
if((myTarget.x==0) && (myTarget.z==0) ) {ent_remove(myTarget);}

-> results in a crash, because myTarget is NULL ...

so first its Not Null, then its Null...
Very good engine programming
Posted By: Superku

Re: how to call ptr_for_handle without "crash" - 03/30/11 02:41

Very good programming.
Posted By: Uhrwerk

Re: how to call ptr_for_handle without "crash" - 03/30/11 12:51

Originally Posted By: Oxy
Right now I have the problem that a removed entity
is not recognized as NULL, but
is like a "ZERO" entity, with positiondata at 0,0,0 for example.

This engine really has some flaws.


if(myTarget!=NULL)
{
if((myTarget.x==0) && (myTarget.z==0) ) {ent_remove(myTarget);}

-> results in a crash, because myTarget is NULL ...

so first its Not Null, then its Null...
Very good engine programming

Removed entities are not NULL. An Entity cannot be NULL. NULL is a value that can be assigned to pointers. Lite-C has no way of finding out where you saved pointers to entities and set them automatically to NULL for you. Your code crashes because myTarget is a vagabonding pointer, not because it is NULL and suddenly not NULL. A value different from NULL does not mean a pointer is necessarily valid.

http://tutorial.3dgamestudio.net/
Posted By: Damocles_

Re: how to call ptr_for_handle without "crash" - 03/30/11 13:16

is there an instruction to let the engine test if
a pointer is valid?

The pointer is known, the engine knows its objects internally.
So it should be a simple step to confirm the validity
of the object by iterating though the list of objects.
Posted By: Uhrwerk

Re: how to call ptr_for_handle without "crash" - 03/30/11 13:37

You can easily do that yourself by iterating over the engine objects with ptr_first (void* object). If the pointer you want to check is amongst the engine objects it is valid, otherwise it is not. However, this is not recommendable in terms of speed and won't work for other memory blocks you have allocated yourself.
© 2024 lite-C Forums