Freesound Command?

BlitzMax Forums/BlitzMax Beginners Area/Freesound Command?

Kippykip(Posted 2013) [#1]
Coming from Blitz3D and plus I noticed BlitzMax has no freesound command.

How can I free a sound from memory?


Kippykip(Posted 2013) [#2]
Found out it was Release or something
Release sound

Gonna try it


Yan(Posted 2013) [#3]
In the default IDE, go to...

Help>Home>Tutorials an articles>BlitzMax overview...

Read, try to comprehend and then come back if you have any questions.


Kryzon(Posted 2013) [#4]
I've asked something similar about four years ago:
http://blitzbasic.com/Community/posts.php?topic=88184

In BlitzMax, resources (an image, a sound, an instance of a type etc.) can only be released from memory with the Garbage Collector.
It's a system that automatically releases resources when they're not being used by your program anymore. It knows which resources won't be used by regarding the amount of references to this resource: the amount of variables pointing to it.

When you do "mySound:TSound = LoadSound(...)," you are assigning a resource (a sound object) to the variable 'mySound.'
While this variable points to the sound resource, the resource won't be released. When you are done using this sound and want it to be released from memory, you need to certify that no variables point to it - that there are no references in your program to this resource.
People usually do this with the "mySound = Null" so that the variable points to something else, a harmless Null. Other ways to do this is to use 'Local' variables that belong to a function or method: When the function or method ends, the local variables are destroyed and therefore they don't point to their resources anymore, which are set for release - in this case you're using the 'scope' to release resources instead of explicitly using "variable = Null."

If there aren't any references to a resource, it'll be set for release by the Garbage Collector.
This isn't done immediately, however. It's an expensive operation, so the Garbage Collector only runs when there's several resources ready for release.

The Garbage Collector releases unused resources automatically (when it "thinks" it's best), but if you want to 'force' the release of whatever resources are unreferenced at a particular moment, you can wait until a non-critical event in your game happens (like when switching from the menu screen to the gameplay etc.) and call GCCollect().
This forces the collection of unreferenced resources. It's something that you can do when you're switching from parts of your program that you know don't share certain resources, and when you're not running any intensive logic in your program such that the collection procedure would cause a slight pause.


Kippykip(Posted 2013) [#5]
Thanks Kryzon for that
That fixes sounds and images, but I think minib3d has a memory leak when loading meshes, textures and other things
because it when doing the = null thing on meshes, it never unloads
even in combination of freeentity
I've experimented with and without etc
memory never clears after going back to the main menu

Don't have this problem in the original Blitz3D version


Azathoth(Posted 2013) [#6]
How are you testing if it unloads or clears the memory?


Kippykip(Posted 2013) [#7]
I look in task manager to see a difference

Currently it is
Function unloadIngame(redoMainmenuToo)
	inGame = False
	StopChannel(CHANNEL_LEVEL)
	CHANNEL_LEVEL = Null
	MUS_LEVEL = Null
	SND_FISTATK = Null
	SND_SHOTGUNATK = Null
	FreeEntity(v_fist)
	FreeEntity(v_pistol)
	FreeEntity(v_shotgun)
	FreeEntity(player)
	FreeEntity(map)
	FreeEntity(camera)
	FreeEntity(skybox)
	FreeTexture(skytexture)
	FreeTexture(PUFFA_IMG)
	FreeTexture(PUFFB_IMG)
	FreeTexture(PUFFC_IMG)
	FreeTexture(PUFFD_IMG)
	FreeTexture(PUFFE_IMG)
	FreeTexture(PUFFF_IMG)
	FreeTexture(TEX_NUKAGE1)
	FreeTexture(TEX_NUKAGE2)
	FreeTexture(TEX_NUKAGE3)
	FreeBrush(BRUSH_NUKAGE1)
	FreeBrush(BRUSH_NUKAGE2)
	FreeBrush(BRUSH_NUKAGE3)
	v_fist = Null
	v_pistol = Null
	v_shotgun = Null
	player = Null
	map = Null
	camera = Null
	skybox = Null
	skytexture = Null
	PUFFA_IMG = Null
	PUFFB_IMG = Null
	PUFFC_IMG = Null
	PUFFD_IMG = Null
	PUFFE_IMG = Null
	PUFFF_IMG = Null
	TEX_NUKAGE1 = Null
	TEX_NUKAGE2 = Null
	TEX_NUKAGE3 = Null
	BRUSH_NUKAGE1 = Null
	BRUSH_NUKAGE2 = Null
	BRUSH_NUKAGE3 = Null
	mapSurfNukage = Null	
	
	unloadObj()
	yBounce# = 0
	bounceSpeed# = 0.035
	bounceUp = 0
	walking = 0
	walkspeed# = 0.3
	momentumSpeedY# = 0
	momentumSpeedX# = 0
	momentumAmount# = 0.03
	currentWeapon = 2
	newWeapon = 0
	weaponChangeTimer# = 0
	aniTextureTimer = 0
	If(redoMainmenuToo)
		initMainmenu()
	EndIf
EndFunction


Experimenting didn't help at all, the original .bb file unloads without memory leaks
Here's how the Blitz3D version does it
Function unloadIngame(redoMainmenuToo)
	inGame = False
	StopChannel(CHANNEL_LEVEL)
	FreeSound(MUS_LEVEL)
	FreeSound(SND_FISTATK)
	FreeSound(SND_SHOTGUNATK)
	FreeEntity(v_fist)
	FreeEntity(v_pistol)
	FreeEntity(v_shotgun)
	FreeEntity(player)
	FreeEntity(map)
	FreeEntity(light)
	FreeEntity(camera)
	FreeEntity(gravitycube)
	FreeEntity(skybox)
	FreeEntity(mesh_testdummy)
	FreeTexture(skytexture)
	FreeTexture(PUFFA_IMG)
	FreeTexture(PUFFB_IMG)
	FreeTexture(PUFFC_IMG)
	FreeTexture(PUFFD_IMG)
	FreeTexture(PUFFE_IMG)
	FreeTexture(PUFFF_IMG)
	unloadObj()
	yBounce# = 0
	bounceSpeed# = 0.035
	bounceUp = 0
	walking = 0
	walkspeed# = 0.3
	momentumSpeedY# = 0
	momentumSpeedX# = 0
	momentumAmount# = 0.03
	currentWeapon = 2
	newWeapon = 0
	weaponChangeTimer# = 0
	If(redoMainmenuToo)
		initMainmenu()
	EndIf
End Function



Azathoth(Posted 2013) [#8]
I look in task manager to see a difference
The garbage collector doesn't release the memory back to the system, instead it will reuse the memory for future objects.
Use GCMemAlloced() to find out how much memory is actually being used.


Kippykip(Posted 2013) [#9]
Well it fills up after quiting to mainmenu and back
and eventually EXCEPTION_ACCESS_VIOLATION after a while
I did notice though that if I have 2 of the same entity it doesn't fill the ram up like blitz3d does however unloading meshes is broken

I wish freeentity would just take it away from ram completely