[Haskell-beginners] FFI foreignPtr construction

PICCA Frederic-Emmanuel frederic-emmanuel.picca at synchrotron-soleil.fr
Wed Jan 27 12:50:17 UTC 2016


Hello,

since the last time I think that I understand how to manage Ptr, now I woud like to masterize the ForeignPtr in order to let haskell managed the life of my C objects.
So I try to create a Geoemtry object like this.

-- data Geometry
newtype Geometry = Geometry (Ptr Geometry) deriving (Show, Storable)

newGeometry :: Factory -> ForeignPtr Geometry
newGeometry f = unsafePerformIO $ do
                  geometry <- c_hkl_factory_create_new_geometry f
                  newForeignPtr c_hkl_geometry_free geometry

foreign import ccall safe "hkl.h hkl_factory_create_new_geometry"
  c_hkl_factory_create_new_geometry :: Factory
                                    -> IO (Geometry)

foreign import ccall safe "hkl.h &hkl_geometry_free"
  c_hkl_geometry_free :: FunPtr (Geometry -> IO ())



the C signature are 

HKLAPI HklGeometry *hkl_factory_create_new_geometry(const HklFactory *self) HKL_ARG_NONNULL(1);

HKLAPI void hkl_geometry_free(HklGeometry *self) HKL_ARG_NONNULL(1);


But when I try to compile this code, I get this error message


1 of 1] Compiling Hkl.C            ( src/Hkl/C.hs, dist/build/Hkl/C.o )

src/Hkl/C.hs:51:33:
    Couldn't match type ‘Geometry’ with ‘Ptr Geometry’
    Expected type: GHC.ForeignPtr.FinalizerPtr Geometry
      Actual type: FunPtr (Geometry -> IO ())
    In the first argument of ‘newForeignPtr’, namely
      ‘c_hkl_geometry_free’
    In a stmt of a 'do' block:
      newForeignPtr c_hkl_geometry_free geometry

src/Hkl/C.hs:51:53:
    Couldn't match expected type ‘Ptr Geometry’
                with actual type ‘Geometry’
    In the second argument of ‘newForeignPtr’, namely ‘geometry’
    In a stmt of a 'do' block:
      newForeignPtr c_hkl_geometry_free geometry

I do not understand what is wrong in my code

thanks if you can help

Frederic 


More information about the Beginners mailing list