[Haskell-cafe] Seeking advice on best practices with FFI

Bulat Ziganshin bulat.ziganshin at gmail.com
Fri May 9 17:52:51 EDT 2008

Hello PJ,

Saturday, May 10, 2008, 1:45:29 AM, you wrote:

> Q: Does that look ok?  Is the db <- newCString leaking ?

yes. use withCString instead

> void  GetStringValues( char* s1, char* s2);

if this functions fills s1 and s2, use alloca and peekCString

one example of (rather generic) wrapper for C functions that accepts
string (c_method) and returns string (c_out_method):

generalDoWithMethod mType c_action method = do
  withCString method $ \c_method -> do
    allocaBytes aMAX_METHOD_STRLEN $ \c_out_method -> do
      ret <- c_action c_method c_out_method
      case ret of
        0 -> peekCString c_out_method
        _ -> fail$ "Unsupported "++mType++" method or error in parameters: "++method

Best regards,
 Bulat                            mailto:Bulat.Ziganshin at gmail.com

More information about the Haskell-Cafe mailing list