[Rcpp-devel] Passing XPtr between functions

Dirk Eddelbuettel edd at debian.org
Wed Jun 24 15:32:33 CEST 2015


On 24 June 2015 at 08:22, Charles Determan wrote:
| Thank you John,
| 
| I am familiar with bigmemory (I am one of the current developers actually). 
| The project I am working on doesn't need the shared memory aspect so was
| intending to avoid the dependency and just leverage the more familiar and
| developed Armadillo library.  However your response informs me that I did not
| fully understand how armadillo objects are handled.  I have some other ideas
| with how I can address my problem but this was something I was hoping to apply
| both for this project and for the sake of learning.

Look at more current RcppArmadillo examples, and in particular _all_ the
variants of the fastLm benchmark in examples.  By doing the explicit cast,
you _forced_ a copy which may then have destroyed the Xptr link.  I *think*
you could just do XPtr around arma::mat innards.  I would actually be
interested in having something somewhere between bigmemory (where I was one
of the first external users) and what we have in RcppArmadillo.

Dirk, during a break of Rcpp class in Zuerich

| Regards,
| Charles
| 
| On Tue, Jun 23, 2015 at 9:29 PM, John Buonagurio <jbuonagurio at exponent.com>
| wrote:
| 
|     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
| 
| 
| 
| _______________________________________________
| Rcpp-devel mailing list
| Rcpp-devel at lists.r-forge.r-project.org
| https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel

-- 
http://dirk.eddelbuettel.com | @eddelbuettel | edd at debian.org


More information about the Rcpp-devel mailing list