[Haskell-cafe] Attach a finalizer to a FunPtr

Matthew Pickering matthewtpickering at gmail.com
Tue Sep 17 08:13:14 UTC 2019


Hi Roman,

I don't believe you can key a weak pointer on an Addr# as it's not a
normal heap allocated object.

Cheers,

Matt

On Tue, Sep 17, 2019 at 8:17 AM Roman Cheplyaka
<roman.cheplyaka at tweag.io> wrote:
>
> Hi all,
>
> I'd like to attach a finalizer to FunPtrs (which point to JIT-compiled functions that need to be deallocated).
>
> However, the act of running the finalizer (no matter what it does) results in a segfault.
>
> Here's a minimal example:
>
> {-# LANGUAGE UnboxedTuples, MagicHash #-}
> import GHC.Exts (FunPtr(..))
> import GHC.Base
> import Foreign.Ptr
> import System.Mem
>
> attachFinalizer :: FunPtr a -> IO () -> IO ()
> attachFinalizer fptr@(FunPtr addr) (IO fin) = IO $ \s0 ->
>   case mkWeak# addr fptr fin s0 of
>     (# s1, _ #) -> (# s1, () #)
>
> foreign import ccall "wrapper"
>   mkIO :: IO () -> IO (FunPtr (IO ()))
>
> main = do
>   fptr <- mkIO $ return ()
>   attachFinalizer fptr $ do
>     -- The exact contents of the finalizer doesn't seem to matter
>     putStrLn "+++ finalizer ran"
>     freeHaskellFunPtr fptr
>   putStrLn "+++ attached successfully"
>   performGC
>
> Is there a proper way to do this? (GHC 8.6.5)
>
> Roman
>
> _______________________________________________
> Haskell-Cafe mailing list
> To (un)subscribe, modify options or view archives go to:
> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
> Only members subscribed via the mailman list are allowed to post.


More information about the Haskell-Cafe mailing list