> The current behaviour of finalizers in GHC is a bit strange:
> 
> -- Strange.hs --------------------------------------------------------
> import Foreign
> import System.Environment
> 
> useMem :: IO ()
> useMem = if length (fib 27) == 0 then return () else return ()
>     where fib :: Int -> [Int]
>           fib 0 = [1]
>           fib 1 = [1]
>           fib n = fib (n-1) ++ fib (n-2)
> 
> main :: IO ()
> main = do
>     args <- getArgs
>     ptr <- malloc
>     newForeignPtr (ptr :: Ptr Int32) (putStrLn "Finalising...")
>     case args of
>        ["1"] -> return ()
>        ["2"] -> putStrLn "Created"
>        ["3"] -> do useMem ; putStrLn "Created"
>        _ -> putStrLn "unknown test"
> ----------------------------------------------------------------------
> panne@jeanluc:~> ghc -O -Wall -o Strange Strange.hs
> panne@jeanluc:~> ./Strange 1
> Finalising...
> panne@jeanluc:~> ./Strange 2
> Created
> panne@jeanluc:~> ./Strange 3
> Finalising...
> Created
> ----------------------------------------------------------------------
> 
> The first and third runs are fine, but why is the finalizer 
> not invoked on the second run?

I think what's going on here is that the finalizer for stdout is running
before your finalizer, and since stdout has already been closed it can't
print out the message :-)

In the first test, stdout hasn't been created at all (it isn't created
until the first use), and in the third test your finalizer gets a chance
to run before the end of the program.  You could also do 'performGC;
yield' to get the same effect.

> The thing I wanted to check in the first place was: Are 
> foreign objects
> really kept alive if they can only be reached via a callback made with
> 'foreign import "wrapper"'? They definitely should be, but I couldn't
> verify this, given the current state of affairs... :-(

Yes, they should be.  The wrapper closure is registered as a StablePtr.

Cheers,
        Simon
_______________________________________________
Cvs-ghc mailing list
[EMAIL PROTECTED]
http://www.haskell.org/mailman/listinfo/cvs-ghc

Reply via email to