> <span>I haven'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'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 <- Thing `fmap` newIORef True</div>
<div> return $ unsafePerformIO ( do</div><div> x' <- evaluate x</div><div> addFinalizer x' $ putStrLn "running finalizer" ) `seq` x</div><div><br></div><div>If anyone can show me how to get rid of unsafePerformIO in there, that'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">> </font><span style>Finalizers are tricky things, especially when combined with some of</span></div>
<span style>> GHC'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"><<a href="mailto:igloo@earth.li" target="_blank">igloo@earth.li</a>></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>
> 64-bit GHC on OS X gives me this:<br>
><br>
> $ ghc -fforce-recomp -threaded finalizer<br>
> [1 of 1] Compiling Main ( finalizer.hs, finalizer.o )<br>
> Linking finalizer ...<br>
> $ ./finalizer<br>
> waiting ...<br>
> done!<br>
> waiting ...<br>
> running finalizer<br>
> done!<br>
><br>
> However, it's a different story when `-O2` is specified:<br>
><br>
> $ ghc -O2 -fforce-recomp -threaded finalizer<br>
> [1 of 1] Compiling Main ( finalizer.hs, finalizer.o )<br>
> Linking finalizer ...<br>
> $ ./finalizer<br>
> waiting ...<br>
> running finalizer<br>
> done!<br>
> waiting ...<br>
> done!<br>
><br>
> This smells like a bug. The stranger thing is that the GC will run the<br>
> finalizer, but it doesn't reclaim the object? I'd think `readIORef`<br>
> going after an invalidated pointer the GC reclaimed would almost<br>
> certainly crash.<br>
<br>
</div></div>The finalizer is attached to the Thing, not the IORef. I haven'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' x) >>= \ix -> ix `seq` return ()<br>
<br>
and define<br>
<br>
{-# NOINLINE ioref' #-}<br>
ioref' :: Thing -> IORef Bool<br>
ioref' = ioref<br>
<br>
then you'll get the sort of output you expect.<br>
<br>
Finalizers are tricky things, especially when combined with some of<br>
GHC'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>