Hi!
Maybe it's too early today 8-) for me, but could it be that the Foreign
interface is a bit *ummh* strange?
If I import PrelForeign( makeForeignObj ), it has the type
makeForeignObj :: Addr -> IO ForeignObj
On the other hand, importing from Foreign( makeForeignObj ) the export
header(!) states (yes, only a comment, but a misleading one...):
makeForeignObj -- :: Addr{-obj-} -> IO ForeignObj
but later in the module:
makeForeignObj :: Addr -> Addr -> IO ForeignObj
makeForeignObj obj finaliser = do
fobj <- PF.makeForeignObj obj
^^^--- NOTE THIS (*)
addForeignFinaliser fobj (app0 finaliser)
return fobj
(*) although the line
--import qualified PrelForeign as PF ( makeForeignObj )
is commented out!
And PrelForeign doesn't provide an 'addForeignFinaliser'...
So, if someone could have a look at the following code snippet, and tell me,
whether there is a nicer way to do this... (at least, the import's don't
make me feel well...)
(Actually, it works, I think:
'foo' does nothing but strdup'ing its argument and returning the pointer to
the newly allocated string.
'foo_free' free's the malloc'ed pointer...
I added some debug output to the C functions. They're freeing the right
memory, and I hope, not too early... :-) (the "memory freed" output does
show up *after* displaying the returned string with "putStr"... )
)
----BEGIN test.lhs
\begin{code}
{-# OPTIONS -fglasgow-exts #-}
import Foreign( addForeignFinaliser )
import PrelForeign( unpackCStringFO, makeForeignObj, ForeignObj )
foreign import "foo_free" unsafe foo_free :: ForeignObj -> IO ()
bar :: [Int] -> IO ForeignObj
bar list = do addr <- _ccall_ foo (show list)
fo <- makeForeignObj addr
addForeignFinaliser fo (foo_free fo)
return fo
main = do r0 <- bar [10000..11999]
putStr $ unpackCStringFO r0
putStrLn "."
\end{code}
----END test.lhs
Confused,
Michael
--
XXXVII:
Ninety percent of the time things will turn out worse than you expect.
The other 10 percent of the time you had no right to expect so much.