> But testing this showed another strange phenomenon: The heap grows
> infinitely. :-( A cut-down version, which has the same effect:
>
> ----------------------------------------------------------------
> import Addr
> import Foreign
>
> main:: IO ()
> main = do
> fo <- mkForeignObj nullAddr
> addForeignFinalizer fo (putStrLn "finalizer")
> main
> ----------------------------------------------------------------
I think this is a red herring. What seems to be happening is that each
finalizer causes a new thread to be created (not exactly optimal, but that's
the way its done at the moment). Most of the threads block, waiting to get
a hold of the stdout handle in order to do the putStrLn. The number of
threads grows faster than it can shrink, so you end up running out of heap.
The way to fix this properly is to have a "finalization thread" which takes
pending finalizers from a queue & runs them. Unless you're careful,
however, you could get starvation and deadlock problems if any of the
finalizers block or otherwise take a long time to run.
Cheers,
Simon