[Rcpp-devel] Read-only property in Rcpp module, can it be a Vector& or must it be a Vector
Douglas Bates
bates at stat.wisc.edu
Sun Jan 30 22:47:41 CET 2011
As the Grateful Dead said, "What a long, strange trip it's been." I
have spent over a month trying to debug some code in the lme4a package
that uses Rcpp modules. Partly I was getting the wrong numerical
answer, which was due to a dumb cut-and-paste error on my part - now
fixed - but more importantly I was getting memory protection errors.
Luke Tierney has added new facilities to the memory allocation and
garbage collection in R-devel to make it easier to detect when
unprotected memory is being accessed. You compile R-devel with
-DTESTING_WRITE_BARRIER to enable this checking.
I found that I was consistently getting an indication of unprotected
memory being accessed in code that looks like
obj1$method1(obj2$method2)
but only after many evaluations of this sequence, even with
gctorture(1) on. obj2$method2 returns a numeric vector, which is the
argument to obj1$method1. The declaration within the module is as a
read-only property
.property("cu", &reModule::cu,
"intermediate solution for u")
and the accessor function is defined as returning a Rcpp::NumericVector &
const Rcpp::NumericVector &cu() const {return d_cu;}
This is starting to seem like the situation that Dominick mentioned
where a reference to an object goes out of scope in the C++ code once
the value is passed to R but R is retaining the SEXP somehow. This is
still pretty murky to me because the underlying SEXP is part of the
Rcpp::NumericVector structure and is protected but somehow the
reference gets stale?
Anyway I am going to change the accessor method to
const Rcpp::NumericVector cu() const {return d_cu;}
or, perhaps
Rcpp::NumericVector cu() const {return clone(d_cu);}
which might cause unnecessary copying but at least will guarantee that
there are no references involved. The reason I would clone d_cu is
because I want to ensure that d_cu cannot be modified at the R level.
So my questions are: Should I avoid returning a reference and always
return the vector itself? Do I need to clone the object can I use
const Rcpp::NumericVector cu() const {return d_cu;}?
More information about the Rcpp-devel
mailing list