Q1: best way to cleanup types?
Blitz3D Forums/Blitz3D Beginners Area/Q1: best way to cleanup types?
| ||
I wonder what is the best way to cleanup types at the end of their use. I see this example: For My.NewType = Each NewType Delete My Next but what if one of the type fields is a handle for an image? Should it be deleted with freeimage, or is it sufficient to delete each instance of the type, as above? thanks |
| ||
Should it be deleted with freeimage, Yep :) |
| ||
delete each newtype that should delete all types of that name |
| ||
delete each newtype that should delete all types of that name But it won't free images/entities that are stored in them. |
| ||
For My.NewType = Each NewType (provided you know that My\Mesh is the handle for meses etc) For My.NewType = Each NewType If My\Mesh>0 FreeEntity My\Mesh EndIf Delete My Next |
| ||
Yeah, you must free any images, sounds, etc, stored in your types, so Malice's code would be necessary there, whereas if it's just a bunch of variables that don't point to any resources you can just 'Delete Each Your_Type'... |
| ||
Ok, so Like Malice's code I would do:Type SpriteType Field graphic Field X# Field Y# End Type For i% = 1 To 100 step 10 My.SpriteType = New SpriteType My\graphic = Spritepic My\X# = 0 + i% My\Y# = 0 Next ;stuff For My.SpriteType = Each SpriteType FreeImage My\graphic Delete My Next I think that is right! |
| ||
You just have to realize that the handle for an image, such as a field in a type, is just an integer which stores the memory address for the image, NOT the image data itself. If you say handle=LoadImage("image.bmp") "handle" is just a number. Similarly, writing My\graphic=LoadImage("image.bmp") just puts a number in "My\graphic." When you delete a type you are just removing the number. Note that there are certain commands which wipe all graphics from memory so when you do that you don't have to deliberately free the graphics. For example, for 3D graphics you just call ClearWorld and then Delete Each [type.] |
| ||
Thanks Joe, so in that case, this would be OK?Spritepic = LoadImage("data/gfx/dodgy_graphic.png") Type SpriteType Field graphic Field X# Field Y# End Type For i% = 1 To 100 step 10 My.SpriteType = New SpriteType My\graphic = Spritepic My\X# = 0 + i% My\Y# = 0 Next ;stuff For My.SpriteType = Each SpriteType Delete My Next FreeImage Spritepic just to be painfully clear and your patience is appreciated... |
| ||
It can be handy to write functions to do this, like this:Type SPRITE Field f_image End Type Function SPRITE_delete(p_sprite.SPRITE) FreeImage(p_sprite\f_image) Delete(p_sprite) End Function Function SPRITE_deleteAll() For sprite.SPRITE = Each SPRITE SPRITE_delete(sprite) Next End Function ; at the end of your program or level you can now say: SPRITE_deleteAll() This may look like extra work, but when you do it like this you assure that what needs to be done is done (when you put it in the function) upon deletion of an object. For instance, suppose you want to keep track of the number of sprites objects. You could add an extra line to the delete function like this: ; suppose this is the global sprite counter g_spriteCounter = 0 Function SPRITE_delete(p_sprite.SPRITE) FreeImage(p_sprite\f_image) Delete(p_sprite) g_spriteCounter = g_spriteCounter - 1 End Function |