[Haskell-beginners] ODP: howto Pipe

PICCA Frederic-Emmanuel frederic-emmanuel.picca at synchrotron-soleil.fr
Sat Feb 20 14:18:46 UTC 2016

In fact I got the segfault also with the P.print...

So I rewrote the solve function like this

withDiffractometer :: Diffractometer -> (Ptr HklEngineList -> IO b) -> IO b
withDiffractometer d fun = do
  let f_engines = difEngineList d
  withForeignPtr f_engines fun

solveTrajPipe' :: Diffractometer -> Pipe Engine Geometry IO ()
solveTrajPipe' dif = flip evalStateT dif $ forever $ do
    -- Inside here we are using `StateT Int (Consumer a IO) r`
    e <- lift await
    dif <- get
    let name = engineName e
    solutions <- lift . lift $ withDiffractometer dif $ \engines ->
     withCString name $ \cname -> do
       engine <- c_hkl_engine_list_engine_get_by_name engines cname nullPtr
       n <- c_hkl_engine_pseudo_axis_names_get engine >>= darrayStringLen
       solve' engine n e >>= getSolution0
    put dif
    lift $ yield solutions

using the evalStateT. It works with million of evaluations :)

but now if I remove the get and put lines, I get the segfault.

This segfault is located in the solve' method

solve' :: Ptr HklEngine -> CSize -> Engine -> IO (ForeignPtr HklGeometryList)
solve' engine n (Engine _ ps _) = do
  let positions = [v | (Parameter _ v _) <- ps]
  withArray positions $ \values ->
      c_hkl_engine_pseudo_axis_values_set engine values n unit nullPtr
      >>= newForeignPtr c_hkl_geometry_list_free

so to my opinion I do not manage correctly the life time of my dif  object, but I do not understand how I can ensure this life time in until the solve' method proceed.



More information about the Beginners mailing list