[gambit-list] Do I understand weak references/tables properly?

Adrien Piérard pierarda at iro.umontreal.ca
Mon Nov 14 00:41:27 EST 2011


Hi,

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)
          (lambda (x)
            (display `(deleting ,x))
            (newline)))

;; show the associated value in the table
(display (table-ref t (vector-ref v 0)))
(newline)
(##gc)
;; delete the key
(vector-set! v 0 #f)
;; call the GC
(##gc)
;; verify the table is empty
(display (table->list t))
(newline)


The output puzzles me:
quarante-deux
(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?

Cheers,

P!


-- 
Français, English, 日本語, 한국어



More information about the Gambit-list mailing list