FreeEntity and FreeTexture problems

Blitz3D Forums/Blitz3D Programming/FreeEntity and FreeTexture problems

Dock(Posted 2004) [#1]
I'm having a bit of trouble with sswift's shadow system at the moment. However, I have managed to track down what the problem is, and I'd really appreciate it if someone could help me fix the bug.

The bug is, basically, that is crashes on the THIRD time it tries to update the static shadows. I tested it, and yes - the first two times it is okay, but the third it gives errors of "Entity does not exist".

	; Delete all the static shadow meshes.
	For ThisShadow.Static_Shadow = Each Static_Shadow
		FreeEntity ThisShadow\Mesh
	Next

	; Delete all the static shadow textures.
	For ThisTexture.Static_Shadow_Texture = Each Static_Shadow_Texture
		FreeTexture ThisTexture\Texture
	Next 

Initially it is "FreeEntity ThisShadow\Mesh" that crashes, whereas if I comment it out, it crashes on "FreeTexture ThisTexture\Texture". If I comment both of them out, then the shadows just get darker and darker each time, as new shadows are created without killing the old ones.

Does anyone have any idea why it would choke on FreeEntity? I even tried this:
"If ThisShadow\Mesh Then FreeEntity ThisShadow\Mesh"

It runs the line, and then crashes. Surely this is checking that the entity exists before it tries to free it, so it seems strange that it crashes.

Help?


GfK(Posted 2004) [#2]
Surely this is checking that the entity exists before it tries to free it
No. FreeEntity does not reset the pointer to 0. All you're doing is checking if the pointer is true (non-zero) or false (zero). It will always return true unless you do 'ThisShadow\Mesh = 0' after FreeEntity.

I'm sure EntityClass could be 'fixed' so it returns a specific value for a non-existent entity instead of throwing a runtime error.


Dock(Posted 2004) [#3]
Gfk - wow, you're right!
All I needed to do was add "ThisShadow\Mesh = 0" (and the same for texture), and it fixed the function entirely. I should probably email Mr Swift.

Thanks a lot, this has been causing me trouble for days! :D


Knotz(Posted 2004) [#4]
From me, my collegues, family, cats and hamsters: Thanks!


sswift(Posted 2004) [#5]
Hey!

I think you did in fact find a bug, but I don't think the problem here is that mesh is not being set to 0. Setting it to 0 might stop the crashing, but it won't fix the actual bug.

The actual bug here is that after I free the mesh and the texture, I failed to delete the type that was containing it. That means that the next time you update, the leftover types, which should no longer exist, are pointing to memory locations that no longer contain meshes.

So the problem is not that the types point to meshes that no longer exist, but that the types themselves still exist.

This modification should fix the problem:

	; Delete all the static shadow meshes.
	For ThisShadow.Static_Shadow = Each Static_Shadow
		FreeEntity ThisShadow\Mesh
		Delete ThisShadow
	Next

	; Delete all the static shadow textures.
	For ThisTexture.Static_Shadow_Texture = Each Static_Shadow_Texture
		FreeTexture ThisTexture\Texture
		Delete ThisTexture
	Next 	


Unless deleting a type element inside a loop that is looping through that type causes the loop to exit prematurely that is... I hope that is not the case.


sswift(Posted 2004) [#6]
Knotz:
Were you having problems with this too?


Rottbott(Posted 2004) [#7]
Sswift, no it doesn't mess up the loop. This is valid code:

For A.Thing = Each Thing
Delete A
Next

It does exactly the same as Delete Each Thing.


Knotz(Posted 2004) [#8]
@swift
No not yet, i just started to use your last system today. Sofar i didn't do any testing.