From nabble.30.miller_2555 at spamgourmet.com  Mon Nov 16 03:16:58 2009
From: nabble.30.miller_2555 at spamgourmet.com (nabble.30.miller_2555 at spamgourmet.com)
Date: Sun, 15 Nov 2009 21:16:58 -0500
Subject: [Rd] Segmentation faults on SEXP conversion
In-Reply-To: <1dd941090911151725w31deb09y354e968b5326c947@mail.gmail.com>
References: <1dd941090911151725w31deb09y354e968b5326c947@mail.gmail.com>
Message-ID: <4B00B61A.3050601@stats.uwo.ca>

On 15/11/2009 8:25 PM, nabble.30.miller_2555 at spamgourmet.com wrote:
> On Sun, Nov 15, 2009 at 2:52 PM, Duncan Murdoch - nabble.30.miller_2555 at spamgourmet.com
> <>
> wrote:
>> On 15/11/2009 1:41 PM, nabble.30.miller_2555 at spamgourmet.com wrote:
>> The "character" type in R corresponds to STRSXP in C, which is a vector of
>> CHARSXPs.  So you need an extra step to get to the C string:
>>
>> const char * omsg = CHAR(STRING_ELT(msg, 0));
>>
>> Duncan Murdoch
>>
> 
> Thank you for the suggestion. I have replaced the code as suggested,
> but I had attempted this conversion earlier. Unfortunately, I still
> receive the same segmentation fault (and backtrace). The underlying
> problem no longer appears to relate to type conversion. The following
> code represents the entirety of the extension's R and C code (and
> NAMESPACE file), and still causes the segmentation fault:

Sorry, I missed something else that's obvious:  .Call needs a return 
value.  The c function needs to include Rinternals.h, and the function 
needs to return a SEXP.  So this works:

#include <Rinternals.h>
SEXP Rwrite() { Rprintf("[%i] %s",12,"Hi");  return R_NilValue; }

(and your ptest function will return NULL).

Duncan Murdoch

> 
> NAMESPACE:
> ---------------------------------------------------
> useDynLib("tstlib")
> export( "ptest" )
> 
> 
> ptest.R:
> ---------------------------------------------------
> ptest <- function()  { .Call("Rwrite", PACKAGE="tstlib");};
> 
> 
> ptest.c:
> ---------------------------------------------------
> #include <R.h>
> void Rwrite() { printf("[%i] %s",12,"Hi"); }
> 
> 
> ptest.R:
> ---------------------------------------------------
> ptest <- function()  { .Call("Rwrite", PACKAGE="tstlib");};
> 
> 
> zzz.R:
> ---------------------------------------------------
> .onLoad <- function(libname, pkgname)
> {
> }
> .onUnload <- function(libpath) {
>     library.dynam.unload("forkex", libpath)
>   }
> 
> 
> This is just about the most simple example I can think of, and don't
> really know why it would segfault (if I change the interface in
> ptest.R above from .Call to .C, no segfault occurs). The following is
> the output from `R CMD SHLIB ptest.c`:
> 
> gcc -m64 -std=gnu99 -I/usr/include/R  -I/usr/local/include    -fpic
> -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions
> -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -c
> ptest.c -o ptest.o
> gcc -m64 -std=gnu99 -shared -L/usr/local/lib64 -o ptest.so ./ptest.o
> -L/usr/lib64/R/lib -lR
>



[Rd] Segmentation faults on SEXP conversion
nabble.30.miller_2555 at spamgourmet.com