> > ForeignObjs are unnecessary! Long live addFinalizer!
> 
> Um, there's a problem with addFinalizer. 

So we stick with addFinalizer for now. As I need to reimplement GdkGC for
the Gtk+HS binding right now, I have a question concering if the following
code is what the 'touch' function should look like:

newtype GC = GC ForeignObj

mkGC :: Addr -> IO GC
mkGC addr = liftM GC $ newForeignObj (gcUnref addr) addr 

unGC :: GC -> Addr
unGC (GC fObj)  = foreignObjToAddr fObj

touchGC GC -> IO ()
touchGC (GC fObj) = seq fObj (return ())

A GC is a structure in C land which should be freed when the GC data type
is garbage collected in Haskell. If we use unGC, we need to touchGC the GC
to make sure it will still be there:

gcCopy :: Window -> GC -> IO GC
gcCopy win source = do
  dest <- gcNew win
  {#call unsafe gc_copy#} (unGC dest) (unGC source)
  touchGC source
  return dest

A general touch function could be

touch :: a -> IO ()
touch a = seq a (return ())

if 'a' is some kind of newtype (like above) or

touch :: (Eq a) => a -> IO ()
touch a = seq (a==a) (return ())

otherwise (though this is slow and probably not necessary). Wasn't there
some talk about a primitive? Why?

Cheers,
Axel.


Reply via email to