Skip to content

Commit 23c8e3e

Browse files
committed
Fix memory leak of z_stream C structure
Spotted by someone else, but I now cannot find the email reporting the issue to me. Sorry! Credit to whoever it was who pointed it out!
1 parent 01192bf commit 23c8e3e

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

Codec/Compression/Zlib/Stream.hsc

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -367,16 +367,16 @@ data State s = State !(ForeignPtr StreamState)
367367

368368
mkState :: ST s (State s)
369369
mkState = unsafeIOToST $ do
370-
ptr <- mallocBytes (#{const sizeof(z_stream)})
371-
#{poke z_stream, msg} ptr nullPtr
372-
#{poke z_stream, zalloc} ptr nullPtr
373-
#{poke z_stream, zfree} ptr nullPtr
374-
#{poke z_stream, opaque} ptr nullPtr
375-
#{poke z_stream, next_in} ptr nullPtr
376-
#{poke z_stream, next_out} ptr nullPtr
377-
#{poke z_stream, avail_in} ptr (0 :: CUInt)
378-
#{poke z_stream, avail_out} ptr (0 :: CUInt)
379-
stream <- newForeignPtr_ ptr
370+
stream <- mallocForeignPtrBytes (#{const sizeof(z_stream)})
371+
withForeignPtr stream $ \ptr -> do
372+
#{poke z_stream, msg} ptr nullPtr
373+
#{poke z_stream, zalloc} ptr nullPtr
374+
#{poke z_stream, zfree} ptr nullPtr
375+
#{poke z_stream, opaque} ptr nullPtr
376+
#{poke z_stream, next_in} ptr nullPtr
377+
#{poke z_stream, next_out} ptr nullPtr
378+
#{poke z_stream, avail_in} ptr (0 :: CUInt)
379+
#{poke z_stream, avail_out} ptr (0 :: CUInt)
380380
return (State stream nullForeignPtr nullForeignPtr 0 0)
381381

382382
runStream :: Stream a -> State s -> ST s (a, State s)

0 commit comments

Comments
 (0)