Question about GC

BlitzMax Forums/BlitzMax Programming/Question about GC

Brendane(Posted 2006) [#1]
I have an object of a type which references an object of another type.

When the first type has no more references and is in the process of being garbage collected - does the GC automatically drop the reference to the second type (and therefore possibly destroy it) - OR, do I have to manually do this myself by setting the reference to Null?

In all my tests it appears that the GC does this automatically, but I always thought it needed to be done manually. Can someone (mark) please give me the definitive answer to this?


Defoc8(Posted 2006) [#2]
well given that you can deal exclusively in base classes..

myshape.shape=new triangle

you may not know what specific type your objects is,
and if it contains references to other type intances..
it would be near impossible to null the fields..
- for this reason alone, i doubt very much if you have
to null object fields to ensure its free'd..well other than
for the object you are directly dealing with..

you could take it a step further, casting all your objects
to "object" - the bmax base class...now you dont know
what any of your objects are...would you really expect to
have to attempt to cast it to every type you have designed to discover what it is, and then null its fields?

hmmm..i dont know, but im assuming everything is taken
care of for you..so long at you null the top level object,
all children will be nulled also.."i hope!" :p ;)


ozak(Posted 2006) [#3]
I think it's handled automatically. You can null field to make sure they're deleted right away, but usually re-assigning variables, leaving scope, etc. would GC objects.


Brendane(Posted 2006) [#4]
Ok, I have examined the destructor assembler code generated by the compiler and I can say definitively that the destructor for your types are generated like this :-

a) Your object's Delete() method is inserted into the destructor code so it is executed first.

b) Next, any object fields in your type have their ref counts decremented and if they reach 0 they are freed also.

c) Next the parent class object destructor is called.

So, it's perfectly fine to leave the object fields without Nulling them. Setting them to Null may cause the GC to collect them earlier but it kind of defeats the object of automatic GC to be writing deletion code.. it's just handy to know about.


marksibly(Posted 2006) [#5]
It's *definitely* done automatically.


Brendane(Posted 2006) [#6]
It's good to know. Thankyou Mark. Any chance of dumping bmax source into the asm files on compilation in the next release?