[plt-scheme] Weak hash tables

From: Don Felgar (dfelgar at rainier-infosys.com)
Date: Thu Sep 9 13:06:24 EDT 2004

I expected this:

  (define wht (make-hash-table 'weak))
  (define strong "bar")
  (hash-table-put! wht 'strong strong)
  (let ((foo "value"))
    (hash-table-put! wht 'key foo))
  (collect-garbage)
  (hash-table-for-each wht
                       (lambda (k v)
                         (display (format "key: ~a value: ~a\n" k v))))

to print:

  key: strong value: bar

but it prints:

  key: strong value: bar
  key: key value: value

I presume this is an artifact of conservative gc.

I wrote the above test to determine if a weak hash table shrinks
during gc, as opposed to accumulating k/v pairs with a #f value.  If
the latter case is true I could periodically purge the hash table, but
I guess adding my own garbage collector wouldn't be much harder.

Any thoughts?

--Don


Posted on the users mailing list.