Freeing Entities and Images

Blitz3D Forums/Blitz3D Programming/Freeing Entities and Images

ChrML(Posted 2003) [#1]
I looked with AvailVidMem() how much memory my video card has left after loading, and unloading, but it seems to always forget to unload something (because I always loose 3 mb video memory, for each time I unload, and load something). This is my code:



Load_Mainmenu.bb:
;Goto start
.load_mainmenu

;Display loadingscreen
ShowLoadingScreen("Data\images\loadscr_main.bmp",showcoords)

;Create camera
camera=CreateCamera()
PositionEntity camera,0,0,0
RotateEntity camera,0,0,0

;Set ambient light
AmbientLight 15,15,15

;Load scene
If FileType("Data\world\mainmenu.x")=0 Then
RuntimeError "Couldn't locate mainmenu.x"
EndIf
scene=LoadMesh("Data\world\mainmenu.x")
PositionEntity scene,0,0,0
RotateEntity scene,0,0,0

;Load mousepointer
LoadMousePointer("Data\images\mouse\menu.bmp","Data\images\mouse\1pic.bmp",255,0,255)

;Load menues
; Validating
If FileType("Data\images\menues\menutop_main.bmp")=0 Then
RuntimeError "Couldn't locate menutop_main.bmp"
EndIf
If FileType("Data\images\menues\menutop_load.bmp")=0 Then
RuntimeError "Couldn't locate menutop_load.bmp"
EndIf
If FileType("Data\images\menues\menu_new.bmp")=0 Then
RuntimeError "Couldn't locate menu_new.bmp"
EndIf
If FileType("Data\images\menues\menu_save.bmp")=0 Then
RuntimeError "Couldn't locate menu_save.bmp"
EndIf
If FileType("Data\images\menues\menu_load.bmp")=0 Then
RuntimeError "Couldn't locate menu_load.bmp"
EndIf
If FileType("Data\images\menues\menu_multi.bmp")=0 Then
RuntimeError "Couldn't locate menu_multi.bmp"
EndIf
If FileType("Data\images\menues\menu_quit.bmp")=0 Then
RuntimeError "Couldn't locate menu_quit.bmp"
EndIf
If FileType("Data\images\menues\loadmenu_gamenames.bmp")=0 Then
RuntimeError "Couldn't locate loadmenu_gamenames.bmp"
EndIf
If FileType("Data\images\menues\menufoot.bmp")=0 Then
RuntimeError "Couldn't locate menufoot.bmp"
EndIf

; Loading
menutop_main=LoadImage("Data\images\menues\menutop_main.bmp")
menutop_load=LoadImage("Data\images\menues\menutop_load.bmp")
menu_new=LoadAnimImage("Data\images\menues\menu_new.bmp",201,20,0,2)
menu_save=LoadAnimImage("Data\images\menues\menu_save.bmp",201,20,0,2)
menu_load=LoadAnimImage("Data\images\menues\menu_load.bmp",201,20,0,2)
menu_quit=LoadAnimImage("Data\images\menues\menu_quit.bmp",201,20,0,2)
menu_multi=LoadAnimImage("Data\images\menues\menu_multi.bmp",201,20,0,2)
loadmenu_gamenames=LoadImage("Data\images\menues\loadmenu_gamenames.bmp")
menufoot=LoadImage("Data\images\menues\menufoot.bmp")

; Masking
MaskImage(menutop_main,255,0,255)
MaskImage(menutop_load,255,0,255)
MaskImage(menu_new,255,0,255)
MaskImage(menu_load,255,0,255)
MaskImage(menu_save,255,0,255)
MaskImage(loadmenu_gamenames,255,0,255)
MaskImage(menu_multi,255,0,255)
MaskImage(menu_quit,255,0,255)
MaskImage(menufoot,255,0,255)

; Menu positions
mainmenux=588
mainmenuy=434
newgamemenux=0
newgamemenuy=0
loadgamemenux=86
loadgamemenuy=225
loadgametextx=187
loadgametexty=230
onlinemenux=0
onlinemenuy=0

;Variables
menux=mainmenux
menuy=mainmenuy
menumode=1
camerawander_main_first=0

;Load fonts
font_gamenames=LoadFont("Alfredo's Dance",20)
font_standard=LoadFont("Arial",16)

;Create lights
Dim light(3000)
light(1)=CreateLight(3)
PositionEntity light(1),-106.893,49.8281,14.2249
RotateEntity light(1),24.1333,-98.297,0
LightRange light(1),400

;Create Dims
Dim save_gamenames$(6)
Dim save_health%(6)
Dim save_checkpoint%(6)

;Hide loadingscreen
Cls
CameraViewport camera,0,0,resx-camleft,resy-camtop

;Main loop
Include "Loop_Mainmenu.bb"

;Unload
.unload_mainmenu
Include "Unload_Mainmenu.bb"

;City Level
.load_city
Include "Load_City.bb"










And this code for unloading ("Unload_Mainmenu.bb"):
;Images
FreeImage menutop_main
FreeImage menutop_load
FreeImage menu_new
FreeImage menu_save
FreeImage menu_load
FreeImage menu_quit
FreeImage menu_multi
FreeImage loadmenu_gamenames
FreeImage menufoot

;World
FreeEntity scene

;Cameraes
FreeEntity camera

;Lights
FreeEntity light(0)

;Fonts
FreeFont font_gamenames
FreeFont font_standard

If leveltoload$="City" Then
Goto load_city
EndIf


Anyone see where the memory loss is?


GfK(Posted 2003) [#2]
Don't forget that your desktop/windows takes up vidmem as well! I have a 32Mb card but the most AvailVidMem ever reports is about 28.5Mb.


ChrML(Posted 2003) [#3]
No, it's not that. Didn't you read that I'm loosing more and more memory for each time I reload inside the game? First when I am at the mainmenu, I have 58 megs left. Then I load the City, now I have 55 which is understandable, cuz it's bigger. Then I load the Mainmenu again, but now I only have 53. Then I load the city again, and have 50 megs. The freeing and loading for the mainmenu is over. Anyone see the memory loss?


fredborg(Posted 2003) [#4]
Assuming your models are textured, you should also free the textures. You can find out which ones are used with the new GetTexture/GetBrush commands, or simply call ClearWorld after freeing all the models.

That should work, try it out!


ChrML(Posted 2003) [#5]
Does ClearWorld free all textures in the models?


ChrML(Posted 2003) [#6]
Hmm, I used ClearWorld() in the unloading script now, and after that, I unloaded all images, but it still looses memory. Any other stuff that can cause it?


ChrML(Posted 2003) [#7]
Bump ^


Graythe(Posted 2003) [#8]
FreeEntity Light(1) should be FreeEntity Light(0) but Clearworld() should cure that.

Include is a thing that should only be executed once per inclusion as there is no way to unload an include.

I hope it helps.


ChrML(Posted 2003) [#9]
Hmm, but all entities as freed in UnloadMainmenu.bb (as shown above), or isn't that possible? When I use include, it copies and pastes the code in, and the loop itself (no loading), is in Loop_Mainmenu.bb. What makes that impossible? Are there any ways to see if an entity exists or not?


Graythe(Posted 2003) [#10]
The last line of code is a goto. At the destination is an include command. I assume the include is being included each time the goto is executed. This may not be the problem but it is the likeliest thing I can see.

;City Level
.load_city
Include "Load_City.bb"

And this code for unloading ("Unload_Mainmenu.bb"):
;Images
FreeImage menutop_main
FreeImage menutop_load
FreeImage menu_new
FreeImage menu_save
FreeImage menu_load
FreeImage menu_quit
FreeImage menu_multi
FreeImage loadmenu_gamenames
FreeImage menufoot

;World
FreeEntity scene

;Cameraes
FreeEntity camera

;Lights
FreeEntity light(0)

;Fonts
FreeFont font_gamenames
FreeFont font_standard

If leveltoload$="City" Then
Goto load_city
EndIf


Ross C(Posted 2003) [#11]
Was it posted somewhere that when freeing entites blitz won't actually free the memory until it's needed, it just deletes the pointer refering to the model. I'm sure that was the case. Just can't find the post anywhere...


Genexi2(Posted 2003) [#12]
Wasnt that for Types Joker?


Gabriel(Posted 2003) [#13]
Yes it was with types. But bear in mind, that you have to make a call to UpdateWorld or RenderWorld ( one of em, I forget which ) before things are freed from video memory. Unless that's changed in a recent update. Certainly used to be that way.