GCCollect wont collect if method called

BlitzMax Forums/BlitzMax Programming/GCCollect wont collect if method called

I dont think this is a bug but more a situation based upon the state of the Call Stack and the CPU Registers.

Local variable Objects are not reference counted so as far as the GC is concerned just setting it to NULL wont force clean up until the Scope from which the varible was declared is popped off stack. That will guaranetee it gets cleaned up next time GCCollect is called.

In context of stact frame it may or may not get cleaned up as you found by adding the additonal code. Due to the way the local stack frame and registers might be effected.

When you declare local varibles inline like your example, the variable exists in the stack context of that function since all a BMAX application is is a function called by the startup code all local variables declared are in scope of that function on the stack.

Calling other functions GCCollect Pushes the resisters on stack so when Garbage Collector scans the stack it might still think the local variabls are alive, as its still on the stack or in a register. Hense the seemingly random behavior.

If you want to see what I mean change the variable to global in your example and what happens.

Play with this example
SuperStrict
Framework BRL.Basic


Type tObject
	Method A:String()
		Return "tObject.A()"
	EndMethod
	
	Method Delete()
		Print "tObject gone!"
	EndMethod
EndType

GCCollect()
Doit()
GCCollect()
End
Function DoIt()
	Print "In Doit"
	Local tObj1:tObject=New tObject
	GCCollect()	' Comment out this line to see changed behavior
	Print tObj1.A()
	tObj1=Null
	GCCollect() 
	Print "Exit Doit"
End Function 


It can be confusing but the GC works just dont assume setting or rely on setting varibable to NULL and calling GCCollect() will imidiately clean up. It will be eventually, but you can rely on it.

Doug Stastny