[Haskell-cafe] Re: hPutStrLn and hFlush

Simon Marlow simonmar at microsoft.com
Tue Jan 10 04:40:41 EST 2006


John Meacham wrote:
> Yeah. this is a major bug in ghc IMHO. I believe it has been fixed, but
> am unsure.

It hasn't been fixed, this is the current behaviour and it's likely to 
stay that way, I'm afraid.

We used to run finalizers on exit, but we stopped doing that for various 
reasons.  Even when we did run finalizers on exit, we couldn't guarantee 
to run all of them.

> Since we can't rely on finalizers to run in general, some
> sort of 'atexit' routine is needed. (which would be a good addition to
> the standard libraries anyway)

You can implement atexit quite straightforwardly, if that's what you want.

exits = unsafePerformIO (newIORef []) :: IORef [IO ()]
main = do_stuff `finally` (readIORef exits >>= sequence_ . reverse)
atexit io = modifyIORef exits (io:)

In reality you probably want to use weak pointers here.  Using this 
version of atexit to close Handles is bad, because it holds on to the 
Handle until the end of the program.  Better to use a Weak pointer and 
allow the Handle to be GC'd, but then you still need finalizers.

Cheers,
	Simon



More information about the Haskell-Cafe mailing list