Skip to content
Prev 2053 / 10988 Next

[Rcpp-devel] Problem passing Armadillo objects back to R

Hmmmmmmmm. I'm afraid I can't fix this.

Here is the story: both wrap(X) and wrap(X) create a new SEXP and 
allocate memory. When wrap(Y) is called, since the created object is 
large, R calls the GC to collect unprotected SEXP to free some space. So 
my guess is that at that point the SEXP that is generated by wrap(X) is 
collected since it is not protected.

I can not deal with this inside ::create, because it is too late.


A Workaround is to use this :

return Rcpp::List::create(
Rcpp::Named("X") = X,
Rcpp::Named("Y") = Y
);

i.e. use the implicit wrap. here wrap will be called much later and 
things are fine.


The issue is that when calling wrap it creates an unprotected SEXP, and 
Rcpp is not responsible for these. If the programmer passes an 
unprotected SEXP to ::create, it is his responsability to protect it. So 
another alternative would be:

SEXP xx = PROTECT( wrap( X ) ) ;
SEXP yy = PROTECT( wrap( Y ) ) ;
Rcpp::List result =  Rcpp::List::create(
Rcpp::Named("X") = xx,
Rcpp::Named("Y") = yy
);
UNPROTECT(2) ;
return res ;

yes, this is ugly.

Romain

Le 06/04/11 11:42, Romain Francois a ?crit :