Finalizer problems..

C.Reinke C.Reinke@ukc.ac.uk
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
release?

Claus

------------- session
Main> y
nothing is
0
Main> x
nothings are
(0,0)
Main> runAllFinalizers
nothing was

Main> y
0
Main> x
(0,0)

------------- 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
     }