Reference counting

John M McIntosh johnmci at ibm.net
Thu Oct 28 08:19:53 UTC 1999


> Hi, John:
> 
> So what's your conclusion about weak references from these
> experiences? What do you think of Squeak's implementation of them,
> which avoids some of the problems you mention below?
>

Mmm you asked... But I'll answer indirectly, mostly because I need more time
to comment, perhaps yet another late late night snack at OOPLSA?

However I took a few hours off of working on my website and looked at
WeakKeyDictionary. I tried the following code.

| weakD aK bK aV bV |

weakD := WeakKeyDictionary new.
aK := String fromString: 'aK'.
bK := String fromString: 'bK'.
aV := String fromString: 'aV'.
bV := String fromString: 'bV'.
weakD at: aK put: aV.
weakD at: bK put: bV.
aK := nil.
bK := nil.
Smalltalk garbageCollect.
Transcript show: weakD printString.

=> WeakKeyDictionary (nil->'bV' nil->'aV' )

Mmmm not quite what I expected. I believe there isn't a call to
WeakArray(class)>>addWeakDependent: so WeakKeyDictionary>>finalizeValues
never is invoked. Is this a bug? So I did

| weakD aK bK aV bV |

weakD := WeakKeyDictionary new.
WeakArray addWeakDependent: weakD.
aK := String fromString: 'aK'.
bK := String fromString: 'bK'.
aV := String fromString: 'aV'.
bV := String fromString: 'bV'.
weakD at: aK put: aV.
weakD at: bK put: bV.
aK := nil.
bK := nil.
Smalltalk garbageCollect.
Transcript show: weakD printString.

=>WeakKeyDictionary ()

--------------------------------
Better. Now lets setup the issue:

| weakD aK bK aV bV |

weakD := WeakKeyDictionary new.
WeakArray addWeakDependent: weakD.
aK := String fromString: 'aK'.
bK := String fromString: 'bK'.
aV := String fromString: 'aV'.
bV := String fromString: 'bV'.
weakD at: aK put: aV.
weakD at: bK put: aK.
aK := nil.
bK := nil.

"Note the two garbage collections"

Smalltalk garbageCollect.
Transcript show: weakD printString.
Smalltalk garbageCollect.
Transcript show: weakD printString.

"Results"

=>WeakKeyDictionary ('aK'->'aV' )
=>WeakKeyDictionary ()

So two GC events are required to cleanup the dictionary, can you guess why?
Can anyone state the solution? Could this cause interesting effects?

--
===========================================================================
John M. McIntosh <johnmci at smalltalkconsulting.com> 1-800-477-2659
Corporate Smalltalk Consulting Ltd.  http://www.smalltalkconsulting.com
===========================================================================
Custom Macintosh programming & various Smalltalk dialects
PGP Key: DSS/Diff/46FC3BE6
Fingerprint=B22F 7D67 92B7 5D52 72D7  E94A EE69 2D21 46FC 3BE6
===========================================================================





More information about the Squeak-dev mailing list