<font size="2">
<p>Hello:</p>
<p>I have a problem with cffi and I don't know if I'm doing something wrong or if It's a bug.</p>
<p>I'm writting a ffi interface to a windowing kit. When testing It I became in problems. The windowing kit has a struct called Rect with the following definition:</p>
<p>struct Rect </p>
<p>{</p>
<p> int x;</p>
<p> int y;</p>
<p> int width;</p>
<p> int height;</p>
<p>};</p>
<p>plus a functional interface to create Rect objects:</p>
<p>Rect app_new_rect(int x , int y , int w , int h ) ;</p>
<p>That function works well because I can run the examples of the toolkit and I see the windows. But when I create the cffi interface, I have problems.</p>
<p>The cffi interface for app_new_rect is:</p>
<p>(cffi:defcstruct Rect</p>
<p> (x :int)</p>
<p> (y :int)</p>
<p> (width :int)</p>
<p> (height :int))</p>
<p> </p>
<p>(cffi:defcfun ("app_new_rect" app_new_rect) Rect</p>
<p> (x :int)</p>
<p> (y :int)</p>
<p> (width :int)</p>
<p> (height :int))</p>
<p>The problem is that when I use the Rect object that this function creates, I get segmentation faults. Ex:</p>
<p>CL-USER> (setq r (app_new_rect 0 0 200 300 ) )</p>
<p>#.(SB-SYS:INT-SAP #X00000000)</p>
<p>CL-USER> (with-foreign-slots ( (x y width height ) r Rect ) </p>
<p> (list x y width height ) </p>
<p> )</p>
<p>Unhandled memory fault at #x0.</p>
<p>[Condition of type SB-SYS:MEMORY-FAULT-ERROR]</p>
<p>Restarts:</p>
<p>0: [ABORT] Return to SLIME's top level.</p>
<p>1: [TERMINATE-THREAD] Terminate this thread (#<THREAD "repl-thread" {1002886941}>)</p>
<p>Backtrace:</p>
<p>0: (SB-SYS:MEMORY-FAULT-ERROR)</p>
<p>1: (SB-SYS:MEMORY-FAULT-ERROR)</p>
<p>2: ("foreign function: call_into_lisp")</p>
<p>3: ("foreign function: post_signal_tramp")</p>
<p>4: (NIL)</p>
<p>5: (SB-INT:SIMPLE-EVAL-IN-LEXENV</p>
<p>(WITH-FOREIGN-SLOTS ((X Y WIDTH HEIGHT) R RECT)</p>
<p>(LIST X Y WIDTH HEIGHT))</p>
<p>#<NULL-LEXENV>)</p>
<p> </p>
<p>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:</p>
<p>(defun rect ( x y width height )</p>
<p> (cffi:with-foreign-object ( ptr 'rect ) </p>
<p> (setf (cffi:foreign-slot-value ptr 'rect 'x ) x )</p>
<p> (setf (cffi:foreign-slot-value ptr 'rect 'y ) y )</p>
<p> (setf (cffi:foreign-slot-value ptr 'rect 'width ) width )</p>
<p> (setf (cffi:foreign-slot-value ptr 'rect 'height ) height )</p>
<p> ptr</p>
<p> )</p>
<p>)</p>
<p>But my surprise is that this doesn't set correctly the values of the slots. See the following output from slime:</p>
<p> </p>
<p>CL-USER> (setq my-little-rectangle (rect 0 0 200 300 ) ) </p>
<p></p>
<p>; in: LAMBDA NIL</p>
<p>; (SETQ MY-LITTLE-RECTANGLE (RECT 0 0 200 300))</p>
<p>; </p>
<p>; caught WARNING:</p>
<p>; undefined variable: MY-LITTLE-RECTANGLE</p>
<p>; </p>
<p>; caught WARNING:</p>
<p>; This variable is undefined:</p>
<p>; MY-LITTLE-RECTANGLE</p>
<p>; </p>
<p>; compilation unit finished</p>
<p>; caught 2 WARNING conditions</p>
<p>#.(SB-SYS:INT-SAP #X2ACC75E76FE8)</p>
<p>CL-USER> (with-foreign-slots ( (x y width height ) my-little-rectangle Rect )</p>
<p>(list x y width height )</p>
<p align="center">)</p>
<p>; in: LAMBDA NIL</p>
<p>; (LET ((#:PTR2816 MY-LITTLE-RECTANGLE))</p>
<p>; (SYMBOL-MACROLET ((X (FOREIGN-SLOT-VALUE #:PTR2816 'RECT 'X))</p>
<p>; (Y (FOREIGN-SLOT-VALUE #:PTR2816 'RECT 'Y))</p>
<p>; (WIDTH (FOREIGN-SLOT-VALUE #:PTR2816 'RECT 'WIDTH))</p>
<p>; (HEIGHT (FOREIGN-SLOT-VALUE #:PTR2816 'RECT 'HEIGHT)))</p>
<p>; (LIST X Y WIDTH HEIGHT)))</p>
<p>; </p>
<p>; caught WARNING:</p>
<p>; undefined variable: MY-LITTLE-RECTANGLE</p>
<p>; </p>
<p>; caught WARNING:</p>
<p>; This variable is undefined:</p>
<p>; MY-LITTLE-RECTANGLE</p>
<p>; </p>
<p>; compilation unit finished</p>
<p>; caught 2 WARNING conditions</p>
<p>(1192261452 0 785098 0)</p>
<p> </p>
<p>The output from the last input should be (0 0 200 300) but I get (1192261452 0 785098 0 ). As the Rect object is used for drawing, nothing works for me. </p>
<p>Please, can anyone explain me what I'm doing wrong? I use an AMD64 on OpenSuse 10.1 x86_64, plus sbcl-1.0.9-x86_64 and cffi_0.9.2 . I've tested also the snapshot cffi-070901 and I get the same problem.</p>
<p>Thank you.</p></font><br clear="all"><br>-- <br>Felip Alàez Nadal