[Rcpp-devel] Idiom for preserving an Rcpp object while passing back an external pointer?

Douglas Bates bates at stat.wisc.edu
Sat Feb 5 15:52:13 CET 2011

As Dirk and Romain know I have been struggling to debug a memory
protection problem that I encounter in code based on Rcpp Modules.  As
with all memory protection problems, it is very difficult to track
down and I have kind-of run out of options right now.

I plan, for the time being, to use code that acts like the
module-based code without going through the modules.  I plan to create
the object in C++ and pass back an external pointer that will be used
to locate the object for later method calls.  Perhaps it is lack of
imagination but I haven't thought of a way to construct an object and
make it persistent until I decide to release the pointer.  The best
way I have been able to derive is to put the C++ object on a global
stack but that approach screams "error-prone".

My question may be somewhat vaguely stated.  What I am trying to avoid
is creating an object in C++ and returning an Rcpp::XPtr then, on
return to R, having the C++ object go out of scope so the external
pointer's target is gone.  Somehow I need to hold on to the C++ object
until the XPtr object is garbage collected.

Suggestions welcome.

More information about the Rcpp-devel mailing list