Skip to content

Commit

Permalink
Fix memory leak in handleToFd, fixes issue #4
Browse files Browse the repository at this point in the history
  • Loading branch information
hasufell committed Jul 10, 2022
1 parent e919c7d commit 176ca32
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 8 deletions.
1 change: 0 additions & 1 deletion System/Posix.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{-# LANGUAGE CPP #-}
{-# LANGUAGE Safe #-}
-----------------------------------------------------------------------------
-- |
-- Module : System.Posix
Expand Down
1 change: 0 additions & 1 deletion System/Posix/ByteString.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{-# LANGUAGE CPP #-}
{-# LANGUAGE Safe #-}
-----------------------------------------------------------------------------
-- |
-- Module : System.Posix.ByteString
Expand Down
1 change: 0 additions & 1 deletion System/Posix/IO.hsc
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
{-# LANGUAGE Safe #-}
-----------------------------------------------------------------------------
-- |
-- Module : System.Posix.IO
Expand Down
1 change: 0 additions & 1 deletion System/Posix/IO/ByteString.hsc
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
{-# LANGUAGE Safe #-}
-----------------------------------------------------------------------------
-- |
-- Module : System.Posix.IO.ByteString
Expand Down
22 changes: 20 additions & 2 deletions System/Posix/IO/Common.hsc
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{-# LANGUAGE CApiFFI #-}
{-# LANGUAGE NondecreasingIndentation #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE Safe #-}

-----------------------------------------------------------------------------
-- |
Expand Down Expand Up @@ -73,7 +72,10 @@ import GHC.IO.Handle.Types
import qualified GHC.IO.FD as FD
import qualified GHC.IO.Handle.FD as FD
import GHC.IO.Exception
import GHC.IO.Buffer
import GHC.IORef
import Data.Typeable (cast)
import System.IO.Unsafe (unsafePerformIO)

#if !defined(HAVE_PIPE)
import System.IO.Error ( ioeSetLocation )
Expand Down Expand Up @@ -301,10 +303,26 @@ handleToFd' h h_@Handle__{haType=_,..} = do
-- converting a Handle into an Fd effectively means
-- letting go of the Handle; it is put into a closed
-- state as a result.
flushWriteBuffer h_

-- free the spare buffers
writeIORef haBuffers BufferListNil
writeIORef haCharBuffer noCharBuffer
writeIORef haByteBuffer noByteBuffer

-- release our encoder/decoder
closeTextCodecs h_

FD.release fd
return (Handle__{haType=ClosedHandle,..}, Fd (FD.fdFD fd))

{-# NOINLINE noCharBuffer #-}
noCharBuffer :: CharBuffer
noCharBuffer = unsafePerformIO $ newCharBuffer 1 ReadBuffer

{-# NOINLINE noByteBuffer #-}
noByteBuffer :: Buffer Word8
noByteBuffer = unsafePerformIO $ newByteBuffer 1 ReadBuffer


-- -----------------------------------------------------------------------------
-- Fd options
Expand Down
1 change: 0 additions & 1 deletion System/Posix/Temp.hsc
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{-# LANGUAGE CApiFFI #-}
{-# LANGUAGE Safe #-}
-----------------------------------------------------------------------------
-- |
-- Module : System.Posix.Temp
Expand Down
1 change: 0 additions & 1 deletion System/Posix/Temp/ByteString.hsc
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{-# LANGUAGE CApiFFI #-}
{-# LANGUAGE Safe #-}
-----------------------------------------------------------------------------
-- |
-- Module : System.Posix.Temp.ByteString
Expand Down

0 comments on commit 176ca32

Please sign in to comment.