The mysteries of GC and WeakArrays....

Peter Smet peter.smet at flinders.edu.au
Tue Mar 23 03:02:33 UTC 1999


I have set up a weakKeyDictionary (wkd) which appears to be working
correctly.
If I say:

wkd at: Object new put: 'this'
the GC reclaims the new Object, since it has no references.

if I create a method as follows:

| b |
b := Bag new.
wkd at: b put: 'this'

and call this method the new Bag is not reclaimed, even though the temp
var | b | has gone out of scope.

However, if I now change the method to read:

| b |
b := Bag new.
wkd at: b put: 'this'.
b := nil.

The b is reclaimed by the GC, and disappears from the wkd.

Is this the correct behaviour? I know temp vars dont have to
be set to nil to be reclaimed, so what is going on here??
If I don't set b to nil, is the b in my wkd ever going to be reclaimed?

How do people program weakCollections such as dependents to
get around this problem ?

Another issue is blocks. Do blocks hold  strong references to
objects, or are all the references held by the enclosing scope?

for eg:
| a b |

a := Set new.
b := [a].
anotherProcedure: b

if the block is passed to another procedure as a Parameter,
does it now hold a strong reference to a, so that a cannot
be GC'd?



Thanks for any help

Peter





More information about the Squeak-dev mailing list