[Rcpp-devel] Passing XPtr between functions

John Buonagurio jbuonagurio at exponent.com
Wed Jun 24 04:29:24 CEST 2015


Hi Charles,

> SEXP testXptr(SEXP A)
> {
>     arma::Mat<double> armaMat = Rcpp::as<arma::Mat<double> >(A);
>     Rcpp::XPtr<double> pMat(armaMat.memptr());
>     return(pMat);
> }

armaMat is on the stack, so the Armadillo memptr is no longer valid when you return from the testXptr function.

One simple solution in your case would be to dynamically allocate with "new" [e.g. arma::mat *A = new arma::mat(...);], though I can't tell you off hand how object lifetime is managed with Armadillo objects.

If you're trying to preserve a matrix across function calls in R, have you looked into bigmemory? http://gallery.rcpp.org/articles/using-bigmemory-with-rcpp/

John



More information about the Rcpp-devel mailing list