[racket] FFI Pass/Return By Value

From: Matthew Flatt (mflatt at cs.utah.edu)
Date: Tue Jun 5 04:25:10 EDT 2012

A struct as a return value should work fine. (That happens frequently
in the Cocoa binding for `racket/gui', for example.)

Can you show more of the program, such as the definitions of `_cpFloat'
and `_cpVect'? In the Chipmunk build that you're using, does `_cpFloat'
correspond to a `float' or a `double'?

At Tue, 5 Jun 2012 00:43:05 -0400, Vince Kuyatt wrote:
> 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.
> 
> ____________________
>   Racket Users list:
>   http://lists.racket-lang.org/users


Posted on the users mailing list.