<div dir="ltr">I think you&#39;re correct, but I still don&#39;t know how to solve it. Any thoughts on that front? I&#39;m at the point of just attaching a finalizer to the statement, or sticking in an IORef to ensure it doesn&#39;t get double-finalized.<br>
<br><div class="gmail_quote">On Mon, Jun 21, 2010 at 2:04 PM, Neil Brown <span dir="ltr">&lt;<a href="mailto:nccb2@kent.ac.uk">nccb2@kent.ac.uk</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">



  

<div text="#000000" bgcolor="#ffffff">
Hi,<br>
<br>
Here&#39;s my guess.  Take a look at this version, and try running it:<br>
<br>
===<br>
{-# LANGUAGE PackageImports #-}<br>
<br>
import qualified &quot;MonadCatchIO-transformers&quot; Control.Monad.CatchIO as C<br>
import Control.Monad.IO.Class<br>
import Control.Monad.Trans.Cont<br>
<br>
<br>
bracket_&#39; :: C.MonadCatchIO m<br>
         =&gt; m a  -- ^ computation to run first (\&quot;acquire resource\&quot;)<br>
         -&gt; m b  -- ^ computation to run last when successful
(\&quot;release resource\&quot;)<br>
         -&gt; m b  -- ^ computation to run last when an exception
occurs<br>
         -&gt; m c  -- ^ computation to run in-between<br>
         -&gt; m c  -- returns the value from the in-between computation<br>
bracket_&#39; before after afterEx thing = C.block $ do<br>
  _ &lt;- before<br>
  r &lt;- C.unblock thing `C.onException` afterEx<br>
  _ &lt;- after<br>
  return r<br>
<br>
<br>
f :: ContT (Either String String) IO String<br>
f = do<br>
    bracket_&#39; (say &quot;acquired&quot;) (say &quot;released-successful&quot;) (say
&quot;released-exception&quot;) (say &quot;executed&quot;)<br>
    say &quot;Hello!&quot;<br>
    () &lt;- error &quot;error&quot;<br>
    return &quot;success&quot;<br>
  where<br>
    say = liftIO . putStrLn<br>
<br>
main :: IO ()<br>
main = flip runContT (return . Right) f &gt;&gt;= print<br>
===<br>
<br>
I get:<br>
<br>
acquired<br>
executed<br>
released-successful<br>
Hello!<br>
released-exception<br>
Tmp.hs: error<br>
<br>
So the exception handler is running after the code that follows the
whole bracket_&#39; call -- and after the bracket_&#39; call has completed
succesfully!<br>
<br>
Here&#39;s my speculation, based on glancing at the libraries involved: I
believe the reason for this may be the MonadCatchIO instance for ContT:<br>
<br>
===<br>
instance MonadCatchIO m =&gt; MonadCatchIO (ContT r m) where<br>
  m `catch` f = ContT $ \c -&gt; runContT m c `catch` \e -&gt; runContT
(f e) c<br>
===<br>
<br>
To my eye, that code takes the continuation to run after the block, c
(which in your case involves the after-action from bracket_, and then
the error), and runs that inside the catch block.  This causes a
successful completion of bracket_ (first release), followed by the
error, which triggers the catch block which then runs the final actions
(second release) and rethrows the error.  Does that sound possible to
anyone else?<br>
<br>
Thanks,<br>
<br>
Neil.<div><div></div><div class="h5"><br>
<br>
On 21/06/10 09:39, Michael Snoyman wrote:
</div></div><blockquote type="cite"><div><div></div><div class="h5">
  <div dir="ltr">Hi cafe,
  <div><br>
  </div>
  <div>I ran into a segfault while working on some database code. I
eventually traced it back to a double-finalizing of a statement (read:
freeing memory twice), which ultimately led back to switching my code
to use the ContT monad transformer. I was able to isolate this down to
a minimal test case (catch.hs); when run, it prints the line &quot;released&quot;
twice.</div>
  <div><br>
  </div>
  <div>In an attempt to understand what&#39;s going on, I rewrote the code
to avoid the libraries entirely (catch-simplified.hs); it didn&#39;t give
me any insight into the problem, but maybe it will help someone else.</div>
  <div><br>
  </div>
  <div>If someone sees an obvious mistake I&#39;m making in my usage of the
bracket_ function, please let me know. Otherwise, I&#39;d really like to
get a fix for this so I can use this library.</div>
  <div><br>
  </div>
  <div>Thanks,</div>
  <div>Michael</div>
  </div>
  </div></div><pre><fieldset></fieldset>
_______________________________________________
Haskell-Cafe mailing list
<a href="mailto:Haskell-Cafe@haskell.org" target="_blank">Haskell-Cafe@haskell.org</a>
<a href="http://www.haskell.org/mailman/listinfo/haskell-cafe" target="_blank">http://www.haskell.org/mailman/listinfo/haskell-cafe</a>
  </pre>
</blockquote>
<br>
</div>

</blockquote></div><br></div>