In the quest of replacing some adventurous C code handling callbacks with
the new FFI, I'm stuck as follows: The FFI documentation tells me to
free the callbacks via freeHaskellFunctionPtr, but there's no such thing
in the 3.03 libs. Browsing throug the RTS reveals a function named
freeAdjustor. Hopefully, this is the one to use instead.

Anyway, the following code snippet is taken from a larger program (using
OpenGL/GLUT and drawing a continuously rotating earth) which dies after
2 GCs with the message

 Entered Forward_Ref 4097ef8c: Should never occur!

I wasn't able to cut it down to a small complete program yet...
(usual setup: GHC 3.03 from current dir, Intel-Linux libc5)
-------------------------------------------------------------------------
...
type UnsignedInt = Word32
type TimerAction = IO ()

foreign import _ccall "freeAdjustor"  freeAdjustor    :: Addr -> IO ()
foreign import _ccall "glutTimerFunc" timerFunc_      :: UnsignedInt -> Addr -> Int -> 
IO ()
foreign export _ccall dynamic         mkTimerCallback :: TimerAction -> IO Addr

timerFunc :: Int -> TimerAction -> IO ()
timerFunc msecs action = do
   adjustor      <- newIORef (error "Foo!")
   timerCallback <- mkTimerCallback (do (readIORef adjustor >>= freeAdjustor) ; action)
   writeIORef adjustor timerCallback
   timerFunc_ (fromIntegral msecs) timerCallback 42
...
update :: IORef Int -> Int -> IO ()
update ref inc = do
   value <- readIORef ref
   writeIORef ref ((value + inc) `mod` 360)
   postRedisplay

-- timer callback: reschedule itself and update planet's angle
timer :: IORef Int -> Int -> Int -> IO ()
timer ref msecs inc = timerFunc msecs (do timer ref msecs inc ; update ref inc)
...
main = do
...
   day  <- newIORef 0
   timer day 50 4
...
   mainLoop -- now GLUT is in command
-------------------------------------------------------------------------

If the the stuff concerning freeAdjustor is removed, the program runs
out of heap space, as expected.

Am I doing something wrong here or is it a bug?

Heading home,
   Sven

-- 
Sven Panne                                        Tel.: +49/89/2178-2235
LMU, Institut fuer Informatik                     FAX : +49/89/2178-2211
LFE Programmier- und Modellierungssprachen              Oettingenstr. 67
mailto:[EMAIL PROTECTED]            D-80538 Muenchen
http://www.pms.informatik.uni-muenchen.de/mitarbeiter/panne

Reply via email to