title: Common Lisp CFFI: pointer to the pointer
body: <p>I am trying to write the CFFI wrapper for <a href="https://computation.llnl.gov/casc/sundials/main.html" rel="nofollow noreferrer">Sundials CVODE</a> library. SWIG was choking on Sundials headers since they are quite interconnected and SWIG couldn't find the right headers, so I did it by hand: a bit laborious but I've managed.</p>
  
  <p>Now I'm trying to test if it works correctly. For now, just simply creating the "problem object" and deleting it. That is where the problem starts. So, the "problem object" is allocated via function</p>
  
  <pre><code>SUNDIALS_EXPORT void *CVodeCreate(int lmm, int iter);
  </code></pre>
  
  <p>For which I created the wrapper:</p>
  
  <pre><code>(cffi:defcfun "CVodeCreate" :pointer
    (lmm :int)
    (iter :int))
  </code></pre>
  
  <p>PS. <code>SUNDIALS_EXPORT</code> (at least on Unix's) is basically nothing.</p>
  
  <p>Now, to destroy the object, Sundials uses its own function:</p>
  
  <pre><code>SUNDIALS_EXPORT void CVodeFree(void **cvode_mem);
  </code></pre>
  
  <p>So, I need to pass it the reference to the object created by <code>CVodeCreate</code>. In C, if my memory is not at fault, I would have done something like <code>CVodeFree(&amp;problem_object)</code>. In CL I've written this wrapper for the function:</p>
  
  <pre><code>(cffi:defcfun "CVodeFree" :void
    (cvode-mem :pointer))
  </code></pre>
  
  <p>So, here <code>COVDE-MEM</code> is a pointer to a pointer. Question is how to get the pointer of the pointer in CL/CFFI? Here is the beginning of the code:</p>
  
  <pre><code>(defvar *p* (cvodecreate 1 2))
  </code></pre>
  
  <p>(PS. Don't worry about the numbers passed to <code>CVODECREATE</code>, they just tell which methods to use, still need to defined constants to make it more readable)</p>
  
  <p>So <code>*P*</code> is something like</p>
  
  <pre><code>#.(SB-SYS:INT-SAP #X7FFFE0007060)
  </code></pre>
  
  <p>If I pass it directly to <code>CVODEFREE</code>, it ends up in error:</p>
  
  <pre><code>CL-USER&gt; (cvodefree *p*)
  ; Evaluation aborted on #&lt;SIMPLE-ERROR "bus error at #X~X" {1005EC9BD3}&gt;.
  </code></pre>
  
  <p>I've tried passing <code>(CFFI:POINTER-ADDRESS *P*)</code> but it results in similar "bus error..." (not even sure if this function returns what I need). I've also tried to do <code>(CFFI:MAKE-POINTER (CFFI:POINTER-ADDRESS *P*))</code>, once again without any success.</p>
  
  <p><a href="https://stackoverflow.com/questions/34646657/how-to-call-native-c-function-with-windows-handle-in-common-lisp-cffi">This question</a> suggests this approach:</p>
  
  <pre><code>(cffi:with-foreign-object (p :pointer)
             (setf (cffi:mem-ref p :pointer) (cvodecreate 1 2))
             (cvodefree p))
  </code></pre>
  
  <p>This works (at least it doesn't throw an error). I think I understand how it works: it creates (allocates the memory for) a pointer-to-a-pointer <code>P</code>, whose <code>MEM-REF</code> (or in C terms would be dereferencing <code>*p</code>) is filled by the result on <code>CVODECREATE</code>. Finally, I'm passing this pointer-to-a-pointer to <code>CVODEFREE</code>, which expects exactly this. Finally, the memory allocated for <code>P</code> is freed once the form finished. Is this the correct approach? And is it the only one I can take?</p>