[cffi-devel] About double pointers and C & operator

Alexandre Paes alex.paes at streetdogstudio.com
Sun Apr 18 18:17:30 UTC 2010


Hi,

For a test project, i was creating a CFFI interface to the OpenCV library
and my limited knowledge of CFFI has left me wondering how to achieve
something.

According to the documentation on the OpenCV wiki, in order to start
capturing from a webcam one would call the following function:

CvCapture* cvCreateCameraCapture (int index)

The CvCapture is an opaque structure without public interface, so i simply
store the pointer returned by the function.
To end the image capturing, one would release the capture through the
function:

void cvReleaseCapture(CvCapture** capture)


On the OpenCV wiki there is a sample program (
http://opencv.willowgarage.com/wiki/CameraCapture) that basically has:


int main()
{
    ...

    CvCapture* capture = cvCaptureFromCAM( CV_CAP_ANY );

    ...

    cvReleaseCapture( &capture );

    ...
}

I've defined the foreign functions as:

(cffi:defcfun ("cvCreateCameraCapture" create-camera-capture) :pointer
  (index :int))

(cffi:defcfun ("cvReleaseCapture" %release-capture) :void
  (capture :pointer))

Then in my program i call 'create-camera-capture and it works, the webcam
lights up and starts capturing. I even managed to simulate the &capture
behavior by renaming the previous declaration of release-capture to
%release-capture and creating a new definition like:

(defun release-capture (capture*)
  (cffi:with-foreign-object (capture** :long)
    (setf (cffi:mem-ref capture** :long)
          (cffi:pointer-address capture*))
    (%release-capture capture**)))

It works but it doesn't feel all that nice, is this the best/correct way of
achieving the same as the C code?

Cheers,

Alex Paes
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mailman.common-lisp.net/pipermail/cffi-devel/attachments/20100418/78ceb1ae/attachment.html>


More information about the cffi-devel mailing list