[Haskell-cafe] FFI woes!

Sebastian Sylvan sebastian.sylvan at gmail.com
Thu Dec 16 02:03:33 EST 2004

On Wed, 15 Dec 2004 22:58:53 -0500, Robert Dockins
<robdockins at fastmail.fm> wrote:
> On Thu, 2004-12-16 at 01:05 +0100, Sebastian Sylvan wrote:
> > Another question!
> >
> > Is there a way to force the garbage collector to kick in?
> >
> > I''m trying to find out if my finalizer gets called correctly but I
> > don't know if the garbage collector is run.
> I'm kind of surprised no one has yet mentioned this, so I will.  Relying
> on finalizers to perform significant clean up actions (ie, anything
> besides memory deallocation) is rather frowned upon.  I think that
> spawing a thread and doing other heavy-duty actions inside a finalizer
> is a bad idea, for several reasons:
> 1) Finalizers are not (some say cannot) be guaranteed to run, even on
> normal program termination, even if you force GC before exiting.

I only need a guarantee that it will be run if the Ptr is no longer
being referenced.

> 2) Finalizers can run when the RTS is in a bizarre state (eg, STDOUT
> might not be avaliable, because it has already been finalized)

Fair enough.

> I would suggest you find some way to accomplish what you want without
> using finalizers.

That would require the user to manually free up resources once they're
not needed anymore. Something which I believe is a bit too low-level
for my tastes.
I basically want the user to be able to just create a sound resource
and then play it, without having to do any book-keeping as to when the
sound resource is not used anymore and can be released.
If there was a way to simply defer GC (like you attatch a function to
an object which can simply deny the GC the right to remove it
depending on its state) then I wouldn't have to do anything
significant in the finalizer. I haven't found a way to do this though,
so the plan is instead to extract the parts of the object which needs
to be kept alive and then keep them alive just long enough to finish
playback and then release them.


Sebastian Sylvan
UIN: 44640862

More information about the Haskell-Cafe mailing list