&gt; <span>I haven&#39;t </span><span>checked, but ...</span><div><font color="#222222" face="arial, sans-serif"><br></font></div><div><font color="#222222" face="arial, sans-serif">I checked, and your solution works. In the context of a larger program, getting NOLINE pragmas in all the right places would be challenging, wouldn&#39;t it?</font></div>


<div><font color="#222222" face="arial, sans-serif"><br></font></div><div><font color="#222222" face="arial, sans-serif">I found a bug report on the GHC Trac [1] in which Simon explains the importance of evaluating the thunk before calling addFinalizer. (Otherwise the finalizer is added to the thunk.) This works:</font></div>

<div><font color="#222222" face="arial, sans-serif"><br></font></div><div><font color="#222222" face="arial, sans-serif"><div>newThing :: IO Thing</div><div>newThing = do</div><div>  x &lt;- Thing `fmap` newIORef True</div>

<div>  return $ unsafePerformIO ( do</div><div>    x&#39; &lt;- evaluate x</div><div>    addFinalizer x&#39; $ putStrLn &quot;running finalizer&quot; ) `seq` x</div><div><br></div><div>If anyone can show me how to get rid of unsafePerformIO in there, that&#39;d be great. Tried a few things to no avail.</div>

</font></div><div><font color="#222222" face="arial, sans-serif"><br></font></div><div><font color="#222222" face="arial, sans-serif">&gt; </font><span style>Finalizers are tricky things, especially when combined with some of</span></div>

<span style>&gt; GHC&#39;s optimisations.</span><div><font color="#222222" face="arial, sans-serif"><br></font></div><div><font color="#222222" face="arial, sans-serif">No kidding!<br></font><div><font color="#222222" face="arial, sans-serif"><br>

</font></div><div><font color="#222222" face="arial, sans-serif">[1] <a href="http://hackage.haskell.org/trac/ghc/ticket/5365">http://hackage.haskell.org/trac/ghc/ticket/5365</a></font></div><div><font color="#222222" face="arial, sans-serif"><br clear="all">


</font>Mike Craig<br><br>
<br><br><div class="gmail_quote">On Thu, Feb 16, 2012 at 4:15 PM, Ian Lynagh <span dir="ltr">&lt;<a href="mailto:igloo@earth.li" target="_blank">igloo@earth.li</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">


<div><div>On Thu, Feb 16, 2012 at 02:55:13PM -0600, Austin Seipp wrote:<br>
&gt; 64-bit GHC on OS X gives me this:<br>
&gt;<br>
&gt; $ ghc -fforce-recomp -threaded finalizer<br>
&gt; [1 of 1] Compiling Main             ( finalizer.hs, finalizer.o )<br>
&gt; Linking finalizer ...<br>
&gt; $ ./finalizer<br>
&gt; waiting ...<br>
&gt; done!<br>
&gt; waiting ...<br>
&gt; running finalizer<br>
&gt; done!<br>
&gt;<br>
&gt; However, it&#39;s a different story when `-O2` is specified:<br>
&gt;<br>
&gt; $ ghc -O2 -fforce-recomp -threaded finalizer<br>
&gt; [1 of 1] Compiling Main             ( finalizer.hs, finalizer.o )<br>
&gt; Linking finalizer ...<br>
&gt; $ ./finalizer<br>
&gt; waiting ...<br>
&gt; running finalizer<br>
&gt; done!<br>
&gt; waiting ...<br>
&gt; done!<br>
&gt;<br>
&gt; This smells like a bug. The stranger thing is that the GC will run the<br>
&gt; finalizer, but it doesn&#39;t reclaim the object? I&#39;d think `readIORef`<br>
&gt; going after an invalidated pointer the GC reclaimed would almost<br>
&gt; certainly crash.<br>
<br>
</div></div>The finalizer is attached to the Thing, not the IORef. I haven&#39;t<br>
checked, but I assume that ioref gets inlined, so effectively (ioref x)<br>
is evaluated early. If you change it to<br>
<br>
    readIORef (ioref&#39; x) &gt;&gt;= \ix -&gt; ix `seq` return ()<br>
<br>
and define<br>
<br>
    {-# NOINLINE ioref&#39; #-}<br>
    ioref&#39; :: Thing -&gt; IORef Bool<br>
    ioref&#39; = ioref<br>
<br>
then you&#39;ll get the sort of output you expect.<br>
<br>
Finalizers are tricky things, especially when combined with some of<br>
GHC&#39;s optimisations.<br>
<br>
<br>
Thanks<br>
<span><font color="#888888">Ian<br>
</font></span><div><div><br>
<br>
_______________________________________________<br>
Glasgow-haskell-users mailing list<br>
<a href="mailto:Glasgow-haskell-users@haskell.org" target="_blank">Glasgow-haskell-users@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/glasgow-haskell-users" target="_blank">http://www.haskell.org/mailman/listinfo/glasgow-haskell-users</a><br>
</div></div></blockquote></div><br></div>
</div>