[racket-dev] Testing whether a procedure gets collected

From: Neil Toronto (neil.toronto at gmail.com)
Date: Sat Dec 1 12:45:13 EST 2012

I'm getting ready to push a change to math/array that fixes a memory 
leak. I've devised a test that I think will determine whether an array's 
procedure gets collected after the array is made strict, but I don't 
know whether it works only by accident. Here it is:


(define: collected? : (Boxof Boolean)  (box #f))

(define arr
   (let ([proc  (λ: ([js : Indexes]) 0)])  ; constant array
     (register-finalizer proc (λ (proc) (set-box! collected? #t)))
     (build-array #() proc)))

(array-strict! arr)
(collect-garbage)
(sleep 0)  ; give finalizers a chance to run?
(check-true (unbox collected?))


This test passes for me now, but will fail if anyone else tries it. What 
worries me is that (sleep 0) is apparently required, meaning that 
finalizers aren't run immediately when garbage is collected.

How can I ensure that the finalizer for `proc' gets run before I test 
the value of `collected?'?

Neil ⊥

Posted on the dev mailing list.