Stefan Westmeier writes:
> Dear GHC users,
>
> can someone please give me any hints that help me to understand
> where the versions of the funktion "createOMSObjectDesignator" differ,
> so that the one does what I intend and the other version does
> not !?
>
> Would this also work without using the MVar ?
>
> I did use GHC 2.01.
I used GHC 2.01 for Solaris and Linux.
Sorry for my first posting concerning ForeignObj's. Both versions do not
work. One version does not call the finalizer function at all and the
other one does call it in my opinion to early.
So can anyone please help me to understand, how to use ForeignObj's to
free something from outside the haskell heap. I tried to find some
examples in the compiler and in haggis but had no success.
I include an example which in my opinion does call the finalizer
function to early. This is the output I get:
stefan@eiger:/tmp/TestIt > ForeignTest
>###> Allocated int 1
>###> collecting anything 1
"### performed GC manually"
"### now I use it, but as you can see it has already been collected"
stefan@eiger:/tmp/TestIt >
Thanks, Stefan.
--
Stefan Westmeier [EMAIL PROTECTED]
Schaphuser Str. 24
D-28307 Bremen vox humana: +(49) 421 424001
Germany fax machina: +(49) 421 424045
=============================== ForeignTest.hs ========================
module Main (main) where
import GHCbase
import PackedString
data OMSObjectDesignator = MkOMSObjectDesignator Int ForeignObj
getORef :: OMSObjectDesignator -> IO Int
getORef (MkOMSObjectDesignator i _ ) = return i
createODes :: Int -> IO OMSObjectDesignator
createODes i = do {
primIOToIO(_ccall_ printAddress i "Allocated int");
finAddr <- primIOToIO (_ccall_ AddrFreeAnything);
fobj <- primIOToIO (makeForeignObj (intToAddr i) finAddr);
return (MkOMSObjectDesignator i fobj)
-- return (MkOMSObjectDesignator i (unsafePerformPrimIO (makeForeignObj
(intToAddr i) finAddr)))
}
testIt = do {
odes <- (return 1 >>= createODes);
primIOToIO(performGC);
print "### performed GC manually";
print "### now I use it, but as you can see it has already been collected";
dummy <- getORef odes;
return ()
}
main = testIt
-- --------------------------------------------------------------
-- Utilities
-- --------------------------------------------------------------
nullAddr :: Addr
nullAddr = ``NULL''
intToAddr :: Int -> Addr
intToAddr i = unsafePerformPrimIO (
_casm_ ``%r = (void *)((int)%0);'' i
)
addrToInt :: Addr -> Int
addrToInt i = unsafePerformPrimIO (
_casm_ ``%r = (int)((void *)%0);'' i
)
=============================== foreign.h ========================
#include <stdlib.h>
typedef void *address;
extern void PrintAddress(address addr,const char *str);
extern address AddrFreeAnything(void);
extern void FreeAnything(address addr);
=============================== foreign.c ========================
#include "foreign.h"
void printAddress(address addr,const char *str)
{
if ( str == NULL )
printf(">###> Print Adress %ld \n",addr);
else
printf(">###> %s %ld \n",str,addr);
}
address AddrFreeAnything(void)
{
return ((void *) (FreeAnything));
};
void FreeAnything(address addr)
{
printf(">###> collecting anything %ld \n",addr);
};
=============================== Makefile ========================
HC = ghc-2.01
HCFLAGS = -syslib ghc -fhaskell-1.3 -fglasgow-exts -concurrent \
-recomp -cpp -hi-diffs \
-H10M -K5M \
$(EXTRA_HC_OPTIONS)
.SUFFIXES : .o .hc .hs
.hs.o:
$(RM) $@
$(HC) -c $< $(HCFLAGS) '-#include "foreign.h"'
.hs.hc:
$(RM) $@
$(HC) -C $< $(HCFLAGS) '-#include "foreign.h"'
ForeignTest : ForeignTest.o foreign.o
$(RM) $@
$(HC) -o $@ $(HCFLAGS) ForeignTest.o foreign.o
foreign.o : foreign.c foreign.h