Handling Errors
Axel Simon
A.Simon at ukc.ac.uk
Thu Apr 24 11:55:17 EDT 2003
On Thu, Apr 24, 2003 at 11:26:18AM -0300, Gustavo Villavicencio wrote:
> Hi all,
>
> Suppose the next fragment
>
> main = newCString s1 >>= \p1 ->
> newCString s2 >>= \p2 ->
> let p3 = hstrL p2 p1
> in peekCString p3 >>= \s ->
> print s
>
> where hstrL is a C function. The question is,
> if newCString fails to get allocation to s1 or s2,
> how must i handle this error?.
If newCString fails to allocate the buffer to store the string, it will
later crash while trying to copy the string into the buffer. The
justification is that malloc hardly fails due to virtual memory. If you
have other function that return a null pointer you should have a look at
the functions in Foreign.Marshal.Error .
BTW if hstrL does not free the two strings your code has a space leak. If
hstrL only uses the string, you can use withCString which uses alloca
(allocation on the stack) which is faster anyway.
Hope this helps,
Axel.
More information about the FFI
mailing list