Noob GC Question

BlitzMax Forums/BlitzMax Beginners Area/Noob GC Question

Sledge(Posted 2007) [#1]
Attentive viewers will know that I found a reason to buy Max, but I'd be lying if I said that my familiarity with OOP was it. The fact that we don't manually delete/free things any more (releasing integer assignments aside) is making me a bit apprehensive. So please consider this...

SuperStrict

Type myType
	Field x:Int
	Field y:Int
	
	Function create(initX:Int,initY:Int)
		Local newMyType:myType = New myType
		newMyType.x=initX
		newMyType.y=initY
		ListAddLast (myTypeList,newMyType)
	End Function
	
	Method destroy()
		ListRemove(myTypeList,Self) 
	End Method
EndType
Global myTypeList:TList = CreateList()

'=========================================

For Local i:Int = 0 To 10
	myType.create(10,20*i)
	Global unListed:myType=New myType
Next


...and tell me do:

(1)Ignoring the 'Global unListed' line (which I'll ask you about in a minute), does this generally hang together as a reasonably solid way to handle object creation and destruction in Max?

(2)Thinking of garbage collection, in order to create and destroy instances there has to be a reference to that instance made in the associated method/function. Does this mean that the last instance created/destroyed will hang about in memory even when it has been removed from its list? Should I be worrying about this or does it all really just take care of itself as long as there is nothing to release?

(3)Looking specifically at that 'Global unListed' line -- does the garbage collector cope with the fact that the previous reference is redundant as the for/next loop iterates, or is this a really dumb thing to do? I'm just curious really because I noticed that going...
Global unListed:myType=New myType
Global unListed:myType=New myType

...makes the compiler squawk whereas B3D would just accept it (presumably trusting you to deallocate the first object before initialising the second).


EDIT:Took out my debug code.


Dreamora(Posted 2007) [#2]
if you allocate a new object the old will be cleaned unless referenced somewhere. but your example won't work. unlisted already exists and cant be defined as globel once again.

To have scoping correctly working, you will need to use strict / superstrict at the top of your files as well. (this will prevent you from doing annoying variable write errors as well)


Sledge(Posted 2007) [#3]
unlisted already exists and cant be defined as globel once again


Oh yes I see; so I can go...
Global unListed:myType=New myType
unListed:myType=New myType
...and it won't leak? Not that I necessarily would, but I want to get my head around what goes on behind the scenes.


GfK(Posted 2007) [#4]
If you did that, you'd only have one myType because GC would get the first one. You wouldn't have an unreferenced copy like you would in, say, Blitz3D.


Sledge(Posted 2007) [#5]
Cool -- I'm sure I've seen the GC referred to as unpredictably periodic before so this is reassuring. (I think I will stop worrying about memory leaks and trying to break the GC now!)


GfK(Posted 2007) [#6]
Well, you can turn off automatic garbage collection and do it manually if you prefer. Personally I don't see the point, though. It seems to work really well.


Grey Alien(Posted 2007) [#7]
I trust the GC too unless you use circular references. Also make sure you use stopchannel to free up oggs from windows memory when you are done playing them.