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

Miro Karpis miroslav.karpis at gmail.com
Mon Mar 17 09:50:56 UTC 2014


I finally managed to fix the seg-fault. Brandon and Branimir you were as
always right ;-) . Problem was in the external function definition. After
changing it to 'stdcall' everything works fine.


for further reference working code below:

foreign import stdcall safe "setmodulestring" c_setmodulestring :: CString
-> CUInt -> CString -> CUInt -> CInt -> IO CInt

--all 3 versions work fine:
setmodulestring param value = do
cParam <- newCString param
cValue <- newCString value
let cParamLength = fromIntegral $ length param ::CUInt
    cValueLength = fromIntegral $ length value ::CUInt
    setVarInArray = (-1)::CInt
result <- c_setmodulestring cParam cParamLength cValue cValueLength
setVarInArray
free cParam
free cValue
return result

setmodulestring2 param value = do
let cParamLength = fromIntegral $ length param ::CUInt
    cValueLength = fromIntegral $ length value ::CUInt
    setVarInArray = (-1)::CInt
B.useAsCString (B.pack param) $ \cParam -> do
B.useAsCString (B.pack value) $ \cValue -> do
result <- c_setmodulestring cParam cParamLength cValue cValueLength
setVarInArray
return result

setmodulestring3 param value =
withCString param $ \cParam -> do
withCString value $ \cValue -> do
result <- c_setmodulestring cParam cParamLength cValue cValueLength
setVarInArray
return result
where
     cParamLength = fromIntegral $ length param ::CUInt
     cValueLength = fromIntegral $ length value ::CUInt
     setVarInArray = (-1)::CInt



thank you cafe, for helping me along this ;-)






On Sun, Mar 16, 2014 at 11:57 PM, Miro Karpis <miroslav.karpis at gmail.com>wrote:

> sorry,...yes it works with -fno-ghci-sandbox (no seg.fault)
>
>
> On Sun, Mar 16, 2014 at 11:47 PM, Brandon Allbery <allbery.b at gmail.com>wrote:
>
>> On Sun, Mar 16, 2014 at 6:38 PM, Miro Karpis <miroslav.karpis at gmail.com>wrote:
>>
>>> after looking a bit more I can see that the ffi call does what it should
>>> do, but it crashes right after that. As mentioned before in ghci everything
>>> runs fine.
>>>
>>
>> I don't think I got an answer to this before: does it still work in ghci
>> if you run it with
>>
>>     ghci -fno-ghci-sandbox
>>
>> ?
>>
>> --
>> brandon s allbery kf8nh                               sine nomine
>> associates
>> allbery.b at gmail.com
>> ballbery at sinenomine.net
>> unix, openafs, kerberos, infrastructure, xmonad
>> http://sinenomine.net
>>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20140317/432fde16/attachment-0001.html>


More information about the Haskell-Cafe mailing list