Opened 11 years ago

Closed 10 years ago

Last modified 9 years ago

#985 closed merge (fixed)

Memory Leak in freeHaskellFunPtr

Reported by: mattcbro@… Owned by: igloo
Priority: normal Milestone: 6.6.1
Component: Compiler Version: 6.6
Keywords: callback, FFI, freeHaskellFunPtr Cc:
Operating System: Windows Architecture: Unknown/Multiple
Type of failure: None/Unknown Test Case: provided
Blocked By: Blocking:
Related Tickets: Differential Rev(s):
Wiki Page:


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.


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) ;


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.

Change History (6)

comment:1 Changed 11 years ago by simonpj

Matthew adds (later email): by the way, why can't I get the callback to actually work in this example? If I for example uncomment the line

	-- pd <- cfunc pcf

and say print out the value of pd, I get garbage values.

Should I have used CDouble? I think for windows HsDouble = double in C land.

comment:2 Changed 11 years ago by igloo

Milestone: 6.6.1

The leak, but not the garbage, happens for me on Linux/amd64.

comment:3 Changed 10 years ago by simonmar

Owner: set to simonmar

I'll look into this.

comment:4 Changed 10 years ago by simonmar

Owner: changed from simonmar to igloo
Type: bugmerge

Fixed. Patch to merge (from 6.6 to HEAD):

Thu Feb  1 13:53:33 GMT 2007  Simon Marlow <>
  * fix memory leak in allocExec/freeExec (see bug #985)

comment:5 Changed 10 years ago by igloo

Resolution: fixed
Status: newclosed


comment:6 Changed 9 years ago by simonmar

Architecture: MultipleUnknown/Multiple
Note: See TracTickets for help on using tickets.