[plt-scheme] FFI: callbacks

From: Eli Barzilay (eli at barzilay.org)
Date: Sun Apr 2 11:50:14 EDT 2006

On Apr  2, Ivan Boulanov wrote:
> I still have trouble with passing callback procedures from Scheme to
> C. The problem is when I pass a Scheme procedure as callback to a C
> dynamic library and then store it to reuse somwhere later, I
> accidentally get a memory access error. It seems to me that the
> callback procedure gets collected straight away the GC is called.

The callback procedure needs to be held in Scheme, to avoid GCing it.

> So, I think it is a fault of MzScheme FFI library.

When a Scheme function is sent to C code, there is no way to know if
the C code will hold on to it or not, so you need to do that when

          ((lambda (x) (x x)) (lambda (x) (x x)))          Eli Barzilay:
                  http://www.barzilay.org/                 Maze is Life!

Posted on the users mailing list.