Help me grok addFinalizer

Antoine Latter aslatter at gmail.com
Thu Feb 16 21:43:10 CET 2012


On Thu, Feb 16, 2012 at 2:04 PM, Michael Craig <mkscrg at gmail.com> wrote:
> When I read the docs for System.Mem.Weak, it all seems to make sense. But
> then this code doesn't run as I expect it to when I turn on
> -threaded: http://hpaste.org/63832 (Expected/actual output are listed in the
> paste.)
>
> I've tried this on 7.4.1 and 7.0.4 with the same results. Can someone
> enlighten me?
>

First off, I'm pretty sure finalizers won't run until the data they
were associated with has been GCd, and GHC doesn't do GCs unless there
is allocation - threadDelay doesn't allocate much, I imagine.

Also, from the docs:

"""
A weak pointer may also have a finalizer of type IO (); if it does,
then the finalizer will be run at most once, at a time after the key
has become unreachable by the program ("dead"). The storage manager
attempts to run the finalizer(s) for an object soon after the object
dies, but promptness is not guaranteed.

It is not guaranteed that a finalizer will eventually run, and no
attempt is made to run outstanding finalizers when the program exits.
Therefore finalizers should not be relied on to clean up resources -
other methods (eg. exception handlers) should be employed, possibly in
addition to finalisers.
"""

Antoine



More information about the Glasgow-haskell-users mailing list