Question

I would like to be able to store a matrix created in R in memory and return the pointer. And then later use the pointer to fetch back the matrix from memory. I am running R version 3.0.1 (2013-05-16) -- "Good Sport" on Ubuntu 13.01 and Rcpp version "0.10.6". I have tried ...

// [[Rcpp::export]]
SEXP writeMemObject(NumericMatrix mat)
{
  XPtr<NumericMatrix> ptr(&mat, true);
  return ptr;
}

// [[Rcpp::export]]
NumericMatrix getMemObject(SEXP ptr)
{
  XPtr<NumericMatrix> out(ptr);
  return wrap(out);
}

# This returns a pointer
x <- writeMemObject(matrix(1.0))

But this fails and crashes R when I try again

getMemObject(x)
Error: not compatible with REALSXP
Was it helpful?

Solution

The pointer you feed to XPtr here is the address of a variable that is local to writeMemObject. Quite naturally you have undefined behavior.

Also, external pointers are typically used for things that are not R objects, and a NumericMatrix is an R object, so that looks wrong.

If however, for some reason you really want an external pointer to a NumericMatrix then you could do something like this:

#include <Rcpp.h>
using namespace Rcpp ;

// [[Rcpp::export]]
SEXP writeMemObject(NumericMatrix mat){
  XPtr<NumericMatrix> ptr( new NumericMatrix(mat), true);
  return ptr;
}

// [[Rcpp::export]]
NumericMatrix getMemObject(SEXP ptr){
  XPtr<NumericMatrix> out(ptr);
  return *out ;
}

So the pointer created by new outlives the scope of the writeMemObject function.

Also, please see the changes in getMemObject, in your version you had:

XPtr<NumericMatrix> out(ptr);
return wrap(out);

You are not dereferencing the pointer, wrap would just be an identity and return the external pointer rather than the pointee I guess you were looking for.

OTHER TIPS

What you describe it pretty much the use case for the bigmemory package. Michael Kane wrote a nice Rcpp Gallery article about its use with Rcpp which should address your questions.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top