[Haskell-cafe] Re: ForeignFunPtr

mf-hcafe-15c311f0c at etc-network.de mf-hcafe-15c311f0c at etc-network.de
Tue Sep 8 13:28:06 EDT 2009

You are right, I forgot about callbacks.  freeHaskellFunPtr is only
for Haskell functions packaged for usage by the foreign code.  Example
from the FFI specification, Section 5.4.2:

type Compare = Int -> Int -> Bool
foreign import ccall "wrapper"
  mkCompare :: Compare -> IO (FunPtr Compare)

If you, say,

x = mkCompare (<)

and at some point decide x is not needed in your foreign code any
more, it has to be freed by hand.

This is the only case in which you are allowed to use
freeHaskellFunPtr.  The (much less interesting) case that I was
thinking about, where FunPtr is used for foreign C functions, usually
requires no finalization.

ForeignPtr is used for foreign objects used by Haskell.  The garbage
collector (gc) can decide whether the object is still in use and, if
it's not, finalize it.

A Haskell function wrapped in a FunPtr is used in the foreign world.
Haskell gc cannot be made responsible for finalizing it.  In fact,
(unless the foreign world has some gc of its own) there is no way for
any compiler of deciding when to finalize at all.  The programmer
needs to decide this by calling freeHaskellFunPtr, just like she needs
to decide when to free mallocked memory in C.

Does that make more sense?  Please kick me again if you are still not
buying it.  :-)


On Sun, Sep 06, 2009 at 02:53:52PM -0300, Maur??cio CA wrote:
> To: haskell-cafe at haskell.org
> From: Maur??cio CA <mauricio.antunes at gmail.com>
> Date: Sun, 06 Sep 2009 14:53:52 -0300
> Subject: [Haskell-cafe] Re: ForeignFunPtr
> Isn't freeHaskellFunPtr a required finalization procedure?
> Maurício
>> the purpose of ForeignPtr is to attach a finalization procedure to the
>> object behind the pointer.  for example, you can have close called
>> aimplicitly whenever the garbage collector finds you don't need a file
>> handle any more.  function pointers do not need finalization.
>>> We have ForeignPtr. Why isn't there a
>>> corresponding ForeignFunPtr?
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
> ** ACCEPT: CRM114 PASS osb unique microgroom Matcher ** CLASSIFY 
> succeeds; success probability: 1.0000  pR: 5.6531
> Best match to file #0 (nonspam.css) prob: 1.0000  pR: 5.6531  Total 
> features in input file: 2752
> #0 (nonspam.css): features: 758386, hits: 2838587, prob: 1.00e+00, pR:   
> 5.65 #1 (spam.css): features: 1686574, hits: 3088399, prob: 2.22e-06, pR: 
>  -5.65 

More information about the Haskell-Cafe mailing list