Hello,

I think I have found a memory leak in GHC/FFI. I tested this with GHC
4.04 patchlevel 0 and 1.  By the way, I'm using GHC on Linux 2.2.12
with gcc 2.7.2.3 on a Pentium Pro.

Here's my example program:


import Addr

foreign export ccall dynamic
        mkFn :: (Int -> IO Int) -> IO Addr

foreign import ccall "freeHaskellFunctionPtr"
        freeHaskellFunctionPtr :: Addr -> IO ()

test_fn :: Int -> IO Int
test_fn i = return i

loop :: Int -> IO ()
loop count =
  do 
     adr <- mkFn test_fn

     freeHaskellFunctionPtr adr
     putStrLn (show count)
     loop (count+1)


main :: IO ()
main = loop 1


Running the above program and watching it's memory usage
shows that the memory usage stabilizes after a few seconds and then
keeps constant (for at least 5 million iterations...) - just as I
expected.


But when I replace
     adr <- mkFn test_fn
with
     adr <- mkFn (test_fn . id)
the memory usage *grows* constantly (until the process is terminated
due to out of memory).
This also happens with
     let f = test_fn . id
     adr <- mkFn f
and
     adr <- mkFn f
     (...)
     where
     f = test_fn . id
and
     adr <- mkFn (\x -> return x)


But there's no memory problem with
     adr <- mkFn f
when
     f = test_fn . id
is declared at the top level.


I made some tests with other arguments to `mkFn' and the result was
always the same: When the argument is *not* a function declared at the
top level, the program continously allocates more memory.



Armin.

Reply via email to