[Newbies] Re: What is a "weak" reference
Klaus D. Witzel
klaus.witzel at cobss.com
Tue Jul 15 22:44:21 UTC 2008
On Tue, 15 Jul 2008 23:21:17 +0200, nicolas cellier wrote:
> While we are at it, here is a lesson i learned recently:
> | weak obj |
> weak := WeakIdentityKeyDictionary new.
> "Create an Object"
> obj := Object new.
> "Add a weak reference to this Object"
> weak at: obj put: (Array with: obj).
> "This DoIt methods points to the Object via it's temporary variable.
> Clear this pointer, so that the Object can eventually be reclaimed"
> obj := nil.
> "Now garbageCollect to reclaim the weak references"
> Smalltalk garbageCollect.
> "Let us see if the Object was reclaimed:"
> ^weak size
> Why the object obj was not reclaimed?
Perhaps because WeakIdentityKeyDictionary (as well as WeakKeyDictionary)
instance variable is not initialized/assigned, as its name suggests
something Weak* ;)
Do (WeakIdentityKeyDictionary new inspect) and check for Weak* things:
none here. This is so in the .image version that I'm using right now,
'Squeak3.10.2', haven't checked others.
> Obviously, the WeakKeyAssociation value is not weak...
> It is a strong pointer and points strongly to obj through the Array...
Or it's just a Weak* instance variable bug :( anybody confirm this?
> Trivial, you might say.
> Well yes, it is just http://bugs.squeak.org/view.php?id=7119 in disguise
> and i find it nasty.
> So be very carfull with Weak references - not only newbs - issues might
> definitely be advanced to track down...
More information about the Beginners