clasp icon indicating copy to clipboard operation
clasp copied to clipboard

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

Open kpoeck opened this issue 4 years ago • 5 comments

(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 avatar Mar 12 '20 08:03 kpoeck

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

kpoeck avatar Mar 12 '20 08:03 kpoeck

#936 also happens here

kpoeck avatar Mar 12 '20 08:03 kpoeck

  • Does not seem to happen with MPS.
  • Since neither Bike nor drmeister seem to have the same issue, it might depend on the bdw-gc version being installed.
  • with `brew info bdw-gc´ we should get the version

kpoeck avatar Mar 19 '20 09:03 kpoeck

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

NIL`

kpoeck avatar Mar 22 '20 20:03 kpoeck

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

kpoeck avatar Mar 22 '20 20:03 kpoeck