clasp-developers / clasp

clasp Common Lisp environment
https://clasp-developers.github.io/
2.56k stars 145 forks source link

Weak hash tables: Symbols are garbage collected too early, even if they have a reference #937

Open kpoeck opened 4 years ago

kpoeck commented 4 years ago
(progn
  (defvar *key-store* nil)
  (defvar *test-weak-table* (make-hash-table :test #'eq :weakness :key))
  (dotimes (x 5)
    (let ((sym (make-symbol (princ-to-string x)))) 
      (pushnew sym *key-store*)
      (setf (gethash sym *test-weak-table*)(list x (1+ x)))))
  (setq * nil ** nil *** nil)
  (dotimes (x 10)(gctools:garbage-collect))
  (pprint *key-store*)
  (maphash #'(lambda(key value)
               (print `(,key ,value))) *test-weak-table*)
  (setq *key-store* nil)
  (dotimes (x 10)(gctools:garbage-collect))
  (maphash #'(lambda(key value)
               (print `(,key ,value))) *test-weak-table*)
  )

(#:|4| #:|3| #:|2| #:|1| #:|0|)
(0 (2 3)) 
(0 (4 5)) 
(0 (3 4)) 
(0 (3 4)) 
(0 (0 1)) 
(0 (0 1)) 
(0 (4 5)) 
(0 (1 2)) 
(0 (2 3)) 
(0 (2 3)) 
(0 (4 5)) 
(0 (3 4)) 
(0 (3 4)) 
(0 (0 1)) 
(0 (0 1)) 
(0 (4 5)) 
(0 (1 2)) 
kpoeck commented 4 years ago

This might not happen in all installations, e.g. drmeister does not seem to observe this

kpoeck commented 4 years ago

936 also happens here

kpoeck commented 4 years ago
kpoeck commented 4 years ago

now gives: `(#:|4| #:|3| #:|2| #:|1| #:|0|)

NIL`

kpoeck commented 4 years ago

this seems to be correct

(progn
  (defvar *key-store* nil)
  (defvar *test-weak-table* (make-hash-table :test #'eq :weakness :key))
  (dotimes (x 5)
    (let ((sym (cons x x))) 
      (pushnew sym *key-store*)
      (setf (gethash sym *test-weak-table*)(list x (1+ x)))))
  (setq * nil ** nil *** nil)
  (dotimes (x 10)(gctools:garbage-collect))
  (pprint *key-store*)
  (maphash #'(lambda(key value)
               (print `(,key ,value))) *test-weak-table*)
  (setq *key-store* nil)
  (dotimes (x 10)(gctools:garbage-collect))
  (maphash #'(lambda(key value)
               (print `(,key ,value))) *test-weak-table*)
  )
((4 . 4) (3 . 3) (2 . 2) (1 . 1) (0 . 0))
((3 . 3) (3 4)) 
((1 . 1) (1 2)) 
((2 . 2) (2 3)) 
((0 . 0) (0 1)) 
((4 . 4) (4 5)) 
NIL