WeakKeyDictionary - does this code look right??

Peter Smet peter.smet at flinders.edu.au
Thu Mar 4 00:29:38 UTC 1999


Thanks for your helpful replies.
Andreas, Your example works fine, but if I
change it slightly, the reference is
no longer released. At what stage
should the variable 'a' below get
garbage collected?

After I have run the code?
After I have closed the Transcript/Workspace?

Do I have to explicitly type a := nil?


| w a |
w _ WeakKeyDictionary new.
a := Object new.
w at: a put: 1.
w at: Object new put: 2.
Smalltalk garbageCollect.
w inspect


-----Original Message-----
From: Raab, Andreas <Andreas.Raab at disney.com>
To: 'squeak at cs.uiuc.edu' <squeak at cs.uiuc.edu>
Date: Thursday, March 04, 1999 5:03 AM
Subject: RE: WeakKeyDictionary - does this code look right??


>> Anyway, when I test the idea, the objects in the WeakKey
>> Dictionary never seem to get garbage collected, even
>> if there are no references to them (from the blocks, inspectors
>> etc.)
>>
>What kind of keys do you use?! Also, having no references from an
>*inspector* does not mean that there are no references to the object. If
you
>want to try if the stuff works at all then do something like:
>
> | w |
> w _ WeakKeyDictionary new.
> w at: Object new put: 1.
> w at: Object new put: 2.
> Smalltalk garbageCollect.
> w inspect
>
>Given that you use the latest VM (e.g., 2.3 - the above code does *not*
work
>with pre-2.3 VMs) the Dictionary should contain 2 nil keys (e.g., the keys
>have been collected).
>
>> What am I doing wrong? I am puzzled by the class comment in
>> WeakKeyDictionary which states:
>>
>> "Clients are responsible to register my instances by WeakArray
>>  such that the appropriate actions can be taken upon loss of any keys."
>>
>See above - what you have after some keys are collected is *multiple* nil
>keys in a dictionary. This can be extremely confusing and is potentially
>dangerous. Therefore, WeakDictionaries should be registered by WeakArray
>class so that they get informed that a key has possibly been removed. You
>may want to look at WeakRegistry - it is doing this kind of action.
>
>  Andreas
>--
>PLEASE NOTE NEW EMAIL ADRESS!
>
>+===== Andreas Raab ========= (andreasr at wdi.disney.com) ==+
>| Walt Disney Imagineering        Phone: +1 818 544 5016  I
>I Glendale, CA                    Fax:   +1 818 544 4544  I
>+======< http://isgwww.cs.uni-magdeburg.de/~raab >========+
>
>





More information about the Squeak-dev mailing list