Tlist & Garbage collection question

BlitzMax Forums/BlitzMax Programming/Tlist & Garbage collection question

nino(Posted 2007) [#1]
Supposing I have two objects - one that is a base type for my game objects which has a pointer to the game..

Type Tmob
Field class,x,y
Field game:Tgame
Field img:Timage
Field destroyed=False

Method update()
	''do stuff with game
EndMethod

Method draw()
	DrawImage(img,x,y)
EndMethod

Method destroy()
	destroyed=True
EndMethod
EndType


and the game which extends Tlist and serves as a collection.


Type Tgame Extends TList
Method play()
	For Local m:Tmob = EachIn Self
		If m.destroyed
			Remove(m)
		Else
			m.update()
			m.draw()
		EndIf
	Next
EndMethod

Method make(class,x,y)
	Local m:Tmob = New Tmob
	m.class=class
	m.x=x
	m.y=y
	m.game=Self
	AddFirst(m)
EndMethod
EndType


When a mob is destroyed and removed from the list will it stick around in memory because it has a link to game or will the garbage collection get rid of it? If it does stick around can I solve this by setting game to nulll or somehow destroying the pointer?


Curtastic(Posted 2007) [#2]
The mob will be freed when it's removed from the list because there is nothing pointing to it. Its fields won't keep itself alive.