[plt-scheme] What am I misunderstanding about weak hash tables?
At Tue, 14 Aug 2007 03:43:52 -0700, Eric Hanchrow wrote:
> The attached file shows some tests involving weak hash tables (and
> weak boxes, too). A couple fail, but I expected them to succeed.
> What am I failing to understand?
Try changing the `weak-string' definition to
(define weak-string (string-copy "I'm a weak string"))
With that change, your tests pass for me.
When you have
(define weak-string "I'm a weak string")
then the value bound to `weak-string' is allocated when the code is
compiled/loaded, and that constant is used for every invocation of the
module. Consequently, simply set!ing `weak-string' (in one particular
instance of the module) doesn't eliminate every reference to the
constant. You might get to it again, for example, with a
`require-for-syntax' of the `weak' module.
Adding the `string-copy' call allocates a new string for each
individual module instance, and the `set!' makes the new string
unreachable.
Matthew