Hannah Schroeter <[EMAIL PROTECTED]> writes: 
> 
> Hello!
> 
> [older mail]
> 
> On Sun, Feb 21, 1999 at 11:47:40PM -0800, Sigbjorn Finne 
> (Intl Vendor) wrote:
> 
> > Sven Panne <[EMAIL PROTECTED]> writes: 
> 
> > ...
> > >    * hClose on a semi-closed handle fails (4.02 has this 
> bug, too):
> 
> > [...]
> 
> > But that's not the case here. By the time you get around to doing
> > the 'hClose', you've read to the end of the file and the semi-closed
> > handle has been closed for you (cf. report.)
> 
> That means if you have a program like this:
> 
> main = do
>   h <- openFile ...
>   c <- hGetContents h
>   do_something c
>   hClose h
> 
> You can't know whether the hClose is correct or not, except if it's
> statically known whether do_something really uses up the 
> whole 'c' or not.
> 

You can avoid that problem by ignoring any exceptions that hClose
raises (alternatively, in a seq. program, check if the handle hasn't
already been closed).

However, hClose is baggage that people carry with them from other
languages. In the vast majority, its use is unecessary since the
garbage collector will properly close it once the handle becomes
garbage. So, unless you rely on prompt closing and/or are using
handles heavily, just don't use hClose. (Well, OK, the Haskell
report doesn't promise you that handles are finalised, so this is
ghc specific. Hopefully other implementations will pick this up
though; not to have to do explicit handle management is preferable).

The implementation of hClose has been changed since this Dark Corner
was discovered, so calling hClose on a handle that is already closed
is now a nop and not an error. 

--sigbjorn

Reply via email to