[racket] Trouble with C->Racket callbacks passing pointers to arrays

From: Matthew Flatt (mflatt at cs.utah.edu)
Date: Mon Aug 20 12:54:36 EDT 2012

At Mon, 20 Aug 2012 20:31:05 +0400, Dmitry Pavlov wrote:
> (define _handler (_fun _int _pointer -> _int))

The `_pointer' C type means a reference to memory that is not managed
by the GC. Change the `_pointer' above to `_gcpointer', since the
handler receives a pointer that is managed by the GC.

> (define-libintegrator myfunc (_fun _int _pointer _handler -> _int))

In contrast, the difference between `_pointer' and `_gcpointer' doesn't
matter here, because there's not really a difference between `_pointer'
and `_gcpointer' for a value flowing into C.

The difference between `_pointer' and `_gcpointer' matters for values
flowing back to Racket (either as a foreign result or as an argument to
a Racket-implemented callback).


Debugging/testing tip: When you have a callback function, try adding
`(collect-garbage)' in the callback to try to expose memory-management
problems. In this case, adding `(collect-garbage)' in the callback
function makes the bad result consistent.


Posted on the users mailing list.