[Rd] Segmentation faults on SEXP conversion

From: <nabble.30.miller_2555_at_spamgourmet.com>
Date: Sun, 15 Nov 2009 13:41:49 -0500


Hello -

I am making a first attempt at writing a simple C++ routine to print out R objects (this is a simple proof-of-concept as part of a larger package development). The relevant C++ routine is as follows:

void Rwrite(SEXP fd, SEXP msg) {
  int *ofd = INTEGER(fd);
  const char * omsg = CHAR(asChar(msg));   printf("[%i] %s",*ofd,omsg);
}

And the corresponding interface in R is as follows:

ptest <- function()
  {
    cfd <- as.integer(2);
    cmsg <- as.character("Hi");
    storage.mode(cfd) <- "integer";
    storage.mode(cmsg) <- "character";
    .Call("Rwrite",

          fd=cfd,
          msg=cmsg,
          PACKAGE="forkex"
       );

  }

Upon running the above code as a call to `ptest()`, I receive a segmentation fault. Building the package as `R CMD check --use-valgrind ...` provides the following memcheck output:

> ptest();
==12591== Invalid read of size 1
==12591== at 0x4D04209: SET_SYMVALUE (in /usr/lib64/R/lib/libR.so)
==12591== by 0x4D01E51: Rf_ReplIteration (in /usr/lib64/R/lib/libR.so)
==12591== by 0x4D020B7: (within /usr/lib64/R/lib/libR.so)
==12591== by 0x4D027CF: run_Rmainloop (in /usr/lib64/R/lib/libR.so)
==12591== by 0x40083A: main (in /usr/lib64/R/bin/exec/R)
==12591== Address 0x9 is not stack'd, malloc'd or (recently) free'd
[2] Hi

The same segmentation fault occurs whether I try passing either the `int` or `character` or both (as in the above). I have not tried other types. However, I have attempted various type conversions in the Rdefines.h header, but cannot avoid the segmentation fault. This is the only code in the proof-of-concept pacakage (aside from useDynLoad related registration structures and the R_init_??? call).

I have a feeling it is something simple, but I am not well versed in R package development at this point. Please let me know if other information is helpful.

I am running Linux 2.6.27.30-170.2.82.fc10.x86_64 with R 2.10.0.

Thanks!



R-devel_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel Received on Sun 15 Nov 2009 - 19:45:25 GMT

This archive was generated by hypermail 2.2.0 : Mon 16 Nov 2009 - 01:50:45 GMT