Finalizer problems..

Thu, 11 Jan 2001 16:58:41 +0000

I would like to run some code when something is 
garbage-collected. Fine, he says, just the job for
finalizers (module Weak). Unfortunately, they don't
seem to offer a general solution, as the code below
demonstrates (presumably, smallish things are copied,
not shared, so that the finalizers run too early?).

Is there a general way to add finalizers to values
of arbitrary type (and have them run when the value,
not just one copy of it, disappears)? Or is this
behaviour -he hopes- a bug, to be fixed in the next


------------- session
Main> y
nothing is
Main> x
nothings are
Main> runAllFinalizers
nothing was

Main> y
Main> x

------------- program
import IOExts
import Weak

y = unsafePerformIO $ 
  do { x<-return 0
     ; putStrLn "nothing is"
     ; addFinalizer x (putStrLn "nothing was")
     ; return x

x = unsafePerformIO $ 
  do { x<-return (0,0)
     ; putStrLn "nothings are"
     ; addFinalizer x (putStrLn "nothings were")
     ; return x