From guillaume.yziquel at citycable.ch  Wed Nov 18 02:45:53 2009
From: guillaume.yziquel at citycable.ch (Guillaume Yziquel)
Date: Wed, 18 Nov 2009 02:45:53 +0100
Subject: [Rd] R_NilValue and segfault.
Message-ID: <4B0351D1.5040105@citycable.ch>

Hello.

I've been trying to wrap up the R_NilValue into OCaml-R. I nevertheless 
get a segfault.

As I gathered, R_NilValue is declared in Rinternals.h:

> /* Special Values */
> LibExtern SEXP  R_NilValue;         /* The nil object */
> LibExtern SEXP  R_UnboundValue;     /* Unbound marker */
> LibExtern SEXP  R_MissingArg;       /* Missing argument marker */

So, in my r_data.c file, I have:

-1- an #include directive.

> #include <Rinternals.h>

-2- the function wrapping R data in OCaml data

> CAMLprim value Val_sexp (SEXP sexp)
> {
>   CAMLparam0();
>   CAMLlocal1(result);
>   result = caml_alloc(1, Abstract_tag);
>   Field(result, 0) = (value) sexp;
>     /* Do not use Val_long in the above statement,
>        as it will drop the top bit. See mlvalues.h. */
>   CAMLreturn(result);
> }

and a function taking no argument (in the sense of OCaml), and returning 
an OCaml object encapsulating a pointer which is R_NilValue. (R_NilValue 
is already a pointer, so we just copy it).

> /* The NULL constant in R... */
> 
> CAMLprim value r_null (value unit) {
>   CAMLparam1(unit);
>   CAMLreturn(Val_sexp(R_NilValue));
> }

OCaml code is straightforward:

> external null_creator : unit -> sexp = "r_null"
> let null = null_creator ()

And I get a segfault:

>         Objective Caml version 3.11.1
> 
> # R.sexptype (R.sexp "1");;
> - : R.sexptype = R.RealSxp
> # R.sexptype Quantmod.getSymbols;;
> - : R.sexptype = R.PromSxp

The two above are fine, but inspecting the type of R.null gives me a 
segfault.

> # R.sexptype R.null;;
> /usr/bin/ocaml-batteries: line 2:  3723 Erreur de segmentation  "/usr/lib/ocaml/batteries/toplevel.top" "/usr/lib/ocaml/batteries/top.ml" $@

Please tell me if there's anything obvious which causes this. There's a 
lot of conditional compilation directives in Rinternals.h, and I'm 
wondering whether or not that might be an issue.

All the best,

-- 
      Guillaume Yziquel
http://yziquel.homelinux.org/



[Rd] R_NilValue and segfault.
Guillaume Yziquel