[cffi-devel] Cannot call function with char** arg

Nikolai Nespor nikolai.nespor at utanet.at
Tue Apr 18 21:05:47 UTC 2006


Hi all,

I'm trying to develop a CFFI binding for librrd, but I'm stuck right at
the begining.

The function signature I'm trying to call is

int    rrd_create(int, char **);

My test C code look's like this

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <rrd.h>

int
main(int argc, char** argv)
{
        char* c[6];

        c[0] = "create";
        c[1] = "test.rrd";
        c[2] = "--step=300";
        c[3] = "DS:a:GAUGE:600:U:U";
        c[4] = "DS:b:GAUGE:600:U:U";
        c[5] = "RRA:AVERAGE:0.5:1:300";

        if (rrd_create(6, c) < 0)
                (void) fprintf(stderr, "librrd call failed: %s\n",
                               rrd_get_error());

        return (0);
}

Using SBCL 0.9.9 and CFFI 0.9.0 on x86 I came up with the following LISP
code (after doing the necessary housekeeping to load librrd)

(defvar *cmd* (list "test.rrd" "--step=300" "DS:a:GAUGE:600:U:U" "DS:b:GAUGE:600:U:U" "RRA:AVERAGE:0.5:1:300"))

(cffi:defcfun "rrd_create" :int (argcount :int) (args :pointer))

(cffi:with-foreign-pointer (ptr (+ (reduce #'+ (map 'vector #'length *cmd*)) (length *cmd*) 6))
  (cffi:lisp-string-to-foreign "dummy" ptr 6) ; librrd needs a dummy fst arg
  (let ((curptr (cffi:inc-pointer ptr 6)))
    (dolist (param *cmd*)
      (let ((size (1+ (length param))))
        (cffi:lisp-string-to-foreign param curptr size)
        (setf curptr (cffi:inc-pointer curptr size)))))
  (rrd-create 6 ptr))

But I get this error

memory fault
   [Condition of type SB-KERNEL::MEMORY-FAULT-ERROR]

Restarts:
  0: [ABORT-REQUEST] Abort handling SLIME request.
  1: [ABORT] Exit debugger, returning to top level.

Backtrace:
  0: (SB-KERNEL::MEMORY-FAULT-ERROR)
  1: ("foreign function: call_into_lisp")
  2: ("foreign function: post_signal_tramp")
  3: ("foreign function: getsubopt")
  4: ("foreign function: getopt_long")
  5: ("foreign function: rrd_create")
  6: (RRD-CREATE 6 #.(SB-SYS:INT-SAP #X08269100))
  7: (NIL)
  8: (SB-INT:EVAL-IN-LEXENV (CFFI-SYS:WITH-FOREIGN-POINTER (PTR (+ # # 6)) (CFFI:LISP-STRING-TO-FOREIGN "dummy" PTR 6) (LET (#) (DOLIST # #)) (RRD-CREATE 6 PTR)) #<NULL-LEXENV>)
 --more--

I tried various other ways but did not suceed and now I'm stuck.

TIA

Nikolai
-- 
Ich verwalte sie. Ich zähle sie und zähle sie wieder.
Das ist nicht leicht. Aber ich bin ein ernsthafter Mann.
\\
 ---> Antoine de Saint-Exupery, "Der kleine Prinz"




More information about the cffi-devel mailing list