[Haskell-cafe] ffi call works fine in ghci but not as ghc compiledsegfault

Miroslav Karpis miroslav.karpis at gmail.com
Fri Mar 14 22:24:23 UTC 2014


Hi, please can you help me with following?

I have a call to an external dll (via ffi) which if executed from ghci works
fine. 
If I compile my code with 'ghc -o fm Mycode.hs -L. -lextdll' and run it I
get  'Segmentation fault/access violation in generated code'.



main = do
let param = "FilePath"
let value = "C:/dev/misc/haskell/services/FM"
result <- liftIO $ FM.setmodulestring param value
return "done"

setmodulestring :: String -> String -> IO CInt
setmodulestring param value = do
let cParamLength = fromIntegral $ length param ::CInt
    cValueLength = fromIntegral $ length value ::CInt
    setVarInArray = (-1)::CInt
alloca $ \cParam -> do
alloca $ \cValue -> do
result <- c_setmodulestring cParam cParamLength cValue cValueLength
setVarInArray
return result

If I try also with following, the behaviour is the same:

setmodulestring2 :: String -> String -> IO CInt
setmodulestring2 param value = do
cParam <- newCWString param
cValue <- newCWString value
let cParamLength = fromIntegral $ length param ::CInt
    cValueLength = fromIntegral $ length value ::CInt
    setVarInArray = (-1)::CInt
result <- c_setmodulestring cParam cParamLength cValue cValueLength
setVarInArray
free cParam
free cValue
return res


Any comments/ideas more than appreciated.

Cheers,
Miro









-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20140314/ed67b4b4/attachment.html>


More information about the Haskell-Cafe mailing list