Finalizers not guaranteed to run before the program exits
In the following code (compiled with compiled with ghc --make -fffi -fvia-c Test.hs) the finalizer never gets called, even after replacing printf with something else:
Test.hs:
module Main where
import Foreign.Ptr
import Foreign.ForeignPtr
import Foreign.Marshal.Utils
foreign import ccall safe "ctest.h &ctest" ctestPtr :: FunPtr (Ptr Int -> IO ())
test :: Int -> IO ()
test i = with i test'
where
test' ptr = do fptr <- newForeignPtr ctestPtr ptr
putStrLn "test"
main = do putStrLn "before test..."
test 33
putStrLn "after test..."
ctest.h:
#include <stdio.h>
static inline void ctest( int *i )
{
printf( "finalizer called with: %d\n", *i );
}
I've asked about this on IRC and the Haskell Cafe mailing list and received confirmation that in GHC there is no guarantee that finalizers will run before the program exits:
http://www.haskell.org/pipermail/haskell-cafe/2007-May/025458.html
The FFI addendum to the Haskell 98 report states in section 5.5 that "The only guarantee [for finalizers] is that the finalizer runs before the program terminates".
The same statement is made in GHC documentation for newForeignPtr.
This could be a bug or merely a documentation issue but something should probably be done about it. I suspect it's a bug considering the guarantee specified in the FFI addendum isn't met.
Trac metadata
Trac field | Value |
---|---|
Version | 6.6.1 |
Type | Bug |
TypeOfFailure | OtherFailure |
Priority | normal |
Resolution | Unresolved |
Component | libraries/base |
Test case | |
Differential revisions | |
BlockedBy | |
Related | |
Blocking | |
CC | |
Operating system | Unknown |
Architecture | Unknown |