Garbage Collection catch-22 with blocks

Peter Smet peter.smet at
Thu Jun 24 11:25:54 UTC 1999

Hi all,

Here's my dilemma.
I am trying to implement
anObject when: #anEvent do: aBlock
using weak references.

I am using a global collection to hang on to all of these objects and their

So, I need to hold onto an Object, and I need to hold onto the block that
the Object wants evaluated some time later. The most straightforward thing
is to put the Object in a WeakKeyDictionary, as the key. If I don't use a
strong reference to the block, the block will get garbage-collected. This
forces me to keep a strong reference to the block somewhere. And now here is
the catch. The block contains a strong reference to the object in my
WeakKeyDictionary (since that is its home context). This defeats the WKD,
since my object will now never get garbage collected. It will always have at
least that block home variable pointing to it.

Are there any elegant solutions to this? One is to force the Object to have
an instVar pointing to the block, then the block itself can be stored in a
WKD, but this is kind of ugly.

I have thought about setting the home context to nil, and restoring it when
the block needs to be evaluated, but this too seems ungainly.

Has anyone solved this circular reference problem before? Are there any
better alternatives?


More information about the Squeak-dev mailing list