#5365: Finalizer running prematurely in ghci
------------------------------------------+---------------------------------
  Reporter:  ekmett                       |          Owner:  simonmar        
      Type:  bug                          |         Status:  closed          
  Priority:  normal                       |      Milestone:                  
 Component:  GHCi                         |        Version:  7.0.3           
Resolution:  invalid                      |       Keywords:  finalizers      
  Testcase:                               |      Blockedby:                  
Difficulty:                               |             Os:  Unknown/Multiple
  Blocking:                               |   Architecture:  Unknown/Multiple
   Failure:  Incorrect result at runtime  |  
------------------------------------------+---------------------------------
Changes (by simonmar):

  * status:  new => closed
  * resolution:  => invalid


Comment:

 You are attaching a finalizer to `y`, which is a thunk.  After `y` is
 evaluated, the thunk is no longer referenced, so the finalizer runs.  (`y`
 might not look like a thunk, but it is in fact a call to
 `unpackCString#`).

 Usually you want to `seq` before calling `addFinalizer` for this reason,
 although note that `seq` doesn't guarantee ordering so you could still get
 unexpected results.  This is probably better:

 {{{
 let y = "foo" in (unsafePerformIO $ do
                     y' <- evaluate y
                     addFinalizer y' (putStrLn $ "deleted")) `seq` y
 }}}

-- 
Ticket URL: <http://hackage.haskell.org/trac/ghc/ticket/5365#comment:3>
GHC <http://www.haskell.org/ghc/>
The Glasgow Haskell Compiler

_______________________________________________
Glasgow-haskell-bugs mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/glasgow-haskell-bugs

Reply via email to