[Haskell-cafe] letting go of file handles and Data.Binary

Duncan Coutts duncan.coutts at worc.ox.ac.uk
Sun Apr 20 17:19:05 EDT 2008


On Sun, 2008-04-20 at 14:02 -0700, Ben wrote:
> hi all --
> 
> using binary 0.4.1 on ghc 6.8.2, vista 64 sp1.  consider the following program:
> 
> import System.Directory
> import Data.Binary
> 
> main = do
>   let dat = [1..10]::[Int]
>       fname = "foo.dat"
>   encodeFile fname dat
>   dat2 <- decodeFile fname
>   print (dat == dat2)
>   removeFile fname
> 
> this throws a permission denied exception, presumably because the file
> is still open when the removeFile is called.  i've grovelled the
> source of Data.Bytestring.Lazy and all but i can't seem to understand
> the "right" way to make this work.  note that i've forced the
> evaluation of dat2 and presumably therefore the filehandle is at least
> half-closed.
> 
> any suggestions?

There was a bug in bytestring-0.9.0.1 where the file handle was not
closed when Data.Bytestring.Lazy.hGetContents reached the end of the
file.

The fix is certainly in version 0.9.0.4. So the above program should
work there. If it still does not then the culprit is probably that your
binary deserialiser is not consuming the whole input.

Remember if you have access to a Handle you can always hClose it
explicitly.

Duncan



More information about the Haskell-Cafe mailing list