[Haskell-cafe] Using unsafePerformIO and free with CString

Brandon Allbery allbery.b at gmail.com
Sun Dec 15 20:42:37 UTC 2013


On Sun, Dec 15, 2013 at 3:32 PM, Nikita Karetnikov <nikita at karetnikov.org>wrote:

>   let n = c_strcmp s' t'
>   -- free s'
>   -- free t'
>   return $ case () of
>     _ | n == 0    -> EQ
>       | n <  0    -> LT
>       | otherwise -> GT
>
> Two questions:
>
> 1. May I safely use unsafePerformIO in such cases?
>

Yes, although you might prefer to use the variant specified by the FFI
standard, unsafeLocalState.


> 2. What’s the proper way of using free here?  If I uncomment the above,
>    the function returns incorrect results.
>

Note that you have not forced evaluation of `n` when you free the
CString-s, so `c_strcmp` has not necessarily been called yet.
Control.Exception.evaluate may be of use here.

-- 
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/20131215/bfac67f2/attachment.html>


More information about the Haskell-Cafe mailing list