[gambit-list] Do I understand weak references/tables properly?
pierarda at iro.umontreal.ca
Mon Nov 14 00:41:27 EST 2011
I have the following code:
;; create a table with weak keys
(define t (make-table weak-keys: #t))
;; create an element, and store it somewhere.
(define v (vector (cons 42 42)))
;; Use that element as a key for a hash-table
(table-set! t (vector-ref v 0) "quarante-deux")
;; make a will to be called when the object is to be garbage collected
(make-will (vector-ref v 0)
(display `(deleting ,x))
;; show the associated value in the table
(display (table-ref t (vector-ref v 0)))
;; delete the key
(vector-set! v 0 #f)
;; call the GC
;; verify the table is empty
(display (table->list t))
The output puzzles me:
(deleting (42 . 42))
(((42 . 42) . quarante-deux))
The action of the will is executed, so this means that my key has been GC'd.
So, why does it still show up in my table? Isn't (##gc) supposed to
remove the key/value from the table, now that the key is not strongly
accessible any more?
Français, English, 日本語, 한국어
More information about the Gambit-list