A weak reference is simply a reference that will not prevent an object from being garbage collected. It is common to use a weak reference in back pointers when you know you are having circularities. It is practically a pattern, when building a hierarchy of objects, to make the parent pointer in parent child type structures a weak reference.
So how do you make a weak reference? You stick a WeakArray in as a holder. So in your parent/child objects you would write something like:
Node>>parent ^ parent ifNotNil: [parent first] ifNil: [parent]
Node>>parent: p parent := p ifNotNil: [WeakArray with: p] ifNil: [p]
thus using a WeakArray as a holder for the parent.
When a weak reference gets garbage collected, its weak references are set to nil first.
On Jul 13, 2008, at 9:15 AM, Rob Rothwell wrote:
Can someone explain what a "weak" vs (I am guessing) "strong" reference is? I have been struggling with Garbage Collection for quite some time now and saw this on another list:
"You'll need to add an instance variable to process and modify fork to record the origin. Currently processes don't remember their ancestor. I recommend you create a special fork that remembers ancestry, rather than modifying the default fork. You will potentially accumulate a lot of garbage otherwise. Also, you might consider making the reference from a process to its parent weak to allow the parent to be GC'ed when it terminates even if it has children."
I, too, am maintaining parent/child relationships in my application and have just been doing something like:
Parent>>createChild |child| child := Child new parent: self. ^child
I am assuming, given my troubles, that this is NOT a weak reference?!
Thank you,
Rob
Beginners mailing list Beginners@lists.squeakfoundation.org http://lists.squeakfoundation.org/mailman/listinfo/beginners