[racket] FFI Pass/Return By Value

From: Vince Kuyatt (vince.raiken at gmail.com)
Date: Tue Jun 5 00:43:05 EDT 2012

I am having a strange issue to do with making an FFI binding to some
simple C functions which immediately result in a segmentation fault.
In these functions, all arguments are pass by value, and all return
values are values, not pointers. I am wondering if there is an issue
with the FFI that getting a struct as a return value is causing some
issues, or passing a cstruct by value. I can think of no other reason
that this could be happening.

For example, I have the simple definition in Racket:
(define cpv
  (get-ffi-obj "_cpv" chipmunk (_fun _cpFloat _cpFloat -> _cpVect)))

where chipmunk is a binding to a valid ffi-lib. This corresponds to
the C function:

static inline cpVect cpv(const cpFloat x, const cpFloat y)
{
        cpVect v = {x, y};
        return v;
}

Calling cpv in Racket at any point will immediately cause a
segmentation fault. Is my theory about why this is happening correct?
Or is there something else going on that I am unaware of? I realize
this is a small function that could easily be re-defined in Racket
(the current FFI bindings to Chipmunk do this, actually, possibly
because the developer ran into the same issue I am), but it seems
important to know why this is happening so as to avoid it in the
future either with this FFI binding, or any others I might put
together.


Posted on the users mailing list.