Memory Leak in freeHaskellFunPtr
When using the "wrapper" technique for creating a Haskell callback function it seems that freeHaskellFunPtr is not properly freeing the resource. Memory can be seen to be clearly ramping up linearly using the Windows XP performance monitor. However the heap profiler does not report this. Here are some simple example programs.
<leaky.hs>
module Main where
import Foreign
import Foreign.Ptr
import Foreign.Storable
-- | A convenient type synonym for storable arrays
type Darr = Ptr (Double)
-- | Function type for mapping doubles to doubles
type Dfunc = Double -> Double
-- | A convenient type synonym for monad containing storable arrays
type IODarr = IO (Darr)
foreign import ccall "wrapper" mkDfunc :: Dfunc -> IO (FunPtr Dfunc)
foreign import ccall "cleaky.h cfunc" cfunc :: (FunPtr Dfunc) -> IO ( Double )
dadd :: Dfunc
dadd x = x + 1.0
getleaky :: Dfunc -> IO ()
getleaky cf = do
pcf <- mkDfunc cf
-- pd <- cfunc pcf
print pcf
freeHaskellFunPtr pcf
main = sequence_ [getleaky dadd | q <- [1..500000]]
Some C code that isn't really used. <cleaky.c>
#include <math.h>
#include <stdio.h>
#include "cleaky.h"
double state = 1.0 ;
double cfunc(DFptr fptr)
{
// printf("In cfunc, fptr: %p\n", fptr) ;
state = (*fptr) (state) ;
// printf("state: %g\n", state) ;
return(state) ;
}
<cleaky.h>
typedef double (*DFptr) (double) ;
double cfunc(DFptr) ;
and the steps I used to compile the program:
ghc -c cleaky.c
ghc -fglasgow-exts -fffi -prof -auto -I. --make leaky.hs cleaky.o
The program can be run using
leaky +RTS -hc -RTS
The leak can be exhibited on windows xp 64 running on an amd 64 x2 cpu or on 32 bit versions of windows xp running on intel's core duo processor. By the way, for some reason I couldn't get the callback to actually work with this simple toy example, although I have used this mechanism in the past. Nevertheless the leak occurs without ever calling the C code.
Trac metadata
Trac field | Value |
---|---|
Version | 6.6 |
Type | Bug |
TypeOfFailure | OtherFailure |
Priority | normal |
Resolution | Unresolved |
Component | Compiler |
Test case | |
Differential revisions | |
BlockedBy | |
Related | |
Blocking | |
CC | |
Operating system | |
Architecture | Multiple |