[Haskell-cafe] FFI woes!

Sebastian Sylvan sebastian.sylvan at gmail.com
Thu Dec 16 01:57:25 EST 2004

Okay. Yet another question. 

I have a datatype called SoundSample which is just a ForeignPtr to an
actual sound resource.
Playing  a resource will in the C library return a handle to the
channel. I use a simliar concept where playing a resource will return
a SamplePlaybackRaw.

The sound resource will get freed up using the appropriate C function
when its finalizer is called.
So to keep the sound resource from being finalized while there still
exists a SamplePlaybackRaw value which is playing I let the
SamplePlaybackRaw type contain the SoundSample.
This way the SoundSample can only be freed if all SamplePlaybackRaws
it has spawned are already dead.

Okay, so far so good.

Here's the problem though. If the SamplePlaybackRaw is currently
playing sound when it gets garbage collected, I want to keep the sound
resource (SoundSample) alive until the playback has finished.

To do this the plan was this:
1. Find a way to attatch a finalizer to SamplePlayback.
2. In that finalizer, spawn a new thread which simply loops and checks
the channel (stored in SamplePlaybackRaw) and touches the ForeignPtr
to the sound resource (also stored in the SamplePlaybackRaw) if the
channel is currently playing.

The first part I accomplished via a series of casts: StablePtr -> Ptr
-> ForeignPtr
To this ForeignPtr I then attatched a finalizer.
This didn't work (the finalizer did get called, but the garbage
collection appears to happen even though the ForeignPtr to the
SamplePlaybackRaw is still referenced). Is there a better way to
attatch a finalizer to a Haskell value?


On Thu, 16 Dec 2004 11:09:47 +1100, Ben Lippmeier
<Ben.Lippmeier at anu.edu.au> wrote:
> System.Mem.performGC?
> 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.
> >
> > /S
> >
> >

Sebastian Sylvan
UIN: 44640862

More information about the Haskell-Cafe mailing list