[cffi-devel] Problems with structs

Mikael Lax mikael.lax at bredband.net
Sat Oct 13 11:49:24 UTC 2007


On Sat, 13 Oct 2007 11:35:40 +0200
"Felip Alàez Nadal" <uu.nix.uu at gmail.com> wrote:

> Hello:
> 
> I have a problem with cffi and I don't know if I'm doing something wrong or
> if It's a bug.
> 
> ...
> 
> plus a functional interface to create Rect objects:
> 
> Rect app_new_rect(int x , int y , int w , int h ) ;
> 

Hi, it looks like this functions returns Rects by value. If section 13 of the CFFI
manual is still up-to-date passing structs by value is not supported by CFFI.


> 
> When I had that problem, I tried to write a Lisp-only function to create
> Rect objects , to not use app_new_rect at all. I wrote the next function:
> 
> (defun rect ( x y width height )
> 
>                (cffi:with-foreign-object ( ptr 'rect )
> 
>                (setf (cffi:foreign-slot-value ptr 'rect 'x ) x )
> 
>                (setf (cffi:foreign-slot-value ptr 'rect 'y ) y )
> 
>                (setf (cffi:foreign-slot-value ptr 'rect 'width ) width )
> 
>                (setf (cffi:foreign-slot-value ptr 'rect 'height ) height )
> 
>                ptr
> 
>          )
> 
> )
> 

The ptr you create in with-foreign-object is only valid in the scope of with-foreign-object, 
so you shouldn't return it outside like that.

Sincerely,
Mikael Lax



More information about the cffi-devel mailing list