[Haskell-cafe] Using unsafePerformIO and free with CString
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
> 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...
More information about the Haskell-Cafe