Running a "final" finaliser

Adrian Hey ahey at
Thu Dec 18 12:21:06 EST 2003


I hope this question isn't too stupid, but I can't find
any obvious way to do this from reading the ghc docs.

What I want to do is call a final foreign function (a
library shutdown routine) when Haskell terminates, but
after all ForeignPtr finalisers have been run.

I suppose I could implement some kind of finaliser
counter so the last finalizer could tell it was the
last finaliser and call the shutdown routine, but this
seems a little awkward, especially as these days the
FFI requires finalisers to be foreign functions.

The other possibility that occurs to me is that I call
performGC at the very end of the program, immediately
before calling the library shutdown routine. But I'm
not too sure whether that will guarantee that all
finalizers have been run even if there are no live
references to foreign objects at that point. (Using
GC as described in the "non-stop Haskell" paper it
seems possible that finalisers won't be run immediately
in response to performGC.)

Is there a better way?

Adrian Hey

More information about the Glasgow-haskell-users mailing list