Skip to content

ForeignPtr leak in ghci

I have a program which creates an infinite list of Storable Vectors and then scans through the list until some condition has been met. When compiled, the program runs in constant space. When run in ghci, however, the program leaks memory.

I can reproduce the behavior with the following module, using both ghc-7.2.1 and ghc-7.4.0.20111219 on Mac OS X 10.7 x86_64. More precisely, I do not see a leak (using top) when the program is

  • Compiled with ghc --make -O2 and run from the command-line
  • Compiled with ghc --make -O0 and run from the command-line
  • Compiled with ghc --make -O0, loaded into ghci and run with :main

But I do see a leak (top shows a growing memory usage) when the program is

  • Loaded uncompiled into ghci and run with :main
  • Compiled with ghc --make -O2 (or -O), loaded into ghci and run with :main.
module Main where

import System.IO.Unsafe
import Foreign.ForeignPtr
import Control.Exception

main = do
    let steps = infinite 256
    evaluate $ myLast steps

myLast :: [a] -> a
myLast [x] = x
myLast (x:xs) = myLast xs

infinite :: Int -> [ForeignPtr ()]
infinite n = let
    xs = makeVector n
    in xs `seq` (xs : infinite n)

makeVector :: Int -> ForeignPtr ()
makeVector n = unsafePerformIO $ mallocForeignPtrBytes n
Trac metadata
Trac field Value
Version 7.2.1
Type Bug
TypeOfFailure OtherFailure
Priority normal
Resolution Unresolved
Component GHCi
Test case
Differential revisions
BlockedBy
Related
Blocking
CC
Operating system
Architecture
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information