[racket-dev] Testing whether a procedure gets collected

From: Robby Findler (robby at eecs.northwestern.edu)
Date: Sat Dec 1 12:58:33 EST 2012

How about using a weak box instead?

Robby

On Sat, Dec 1, 2012 at 11:45 AM, Neil Toronto <neil.toronto at gmail.com> wrote:
> 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 ⊥
> _________________________
>  Racket Developers list:
>  http://lists.racket-lang.org/dev


Posted on the dev mailing list.