Is this a circular reference?

BlitzMax Forums/BlitzMax Beginners Area/Is this a circular reference?

Czar Flavius(Posted 2009) [#1]
Is this a circular reference? (For garbage collection purposes) Here is just a simple pseudoexample of a design pattern I find myself using a lot.

Type TFamily
	Field name:String
	Field members:TList 'a list of TPerson
End Type

Type TPerson
	Field name:string
	Field age:Int
	Field myfamily:TFamily
End Type



Scienthsine(Posted 2009) [#2]
Imo automatic garbage collection leads to just as many memory leaks as before due to stuff like this...

You need to remove all instances of a TPerson instance in the TFamily instances when deleting a TPerson instance. When deleting a TFamily instance, you need to make sure no TPerson instances myfamily field is a reference to the TFamily instance being deleted...

ya...

Basically, if your in doubt, or want to keep good practice, always clear lists, and nullify object pointers when deleting stuff. My opinion of garbage collection is to pretend it doesn't exist, except with the obvious lack of calling a function to free blocks of memory.

Does that make sense, or am I too tired?


_Skully(Posted 2009) [#3]
I do that all the time...

or keep a list in the TPerson as well that references all family's he belongs to so you know where to iterate and remove it from in the case of a... erm... Tdeath?