[Rcpp-devel] RInside destructor not working properly?

Dirk Eddelbuettel edd at debian.org
Mon Aug 27 15:36:57 CEST 2012

Hi Christian,

On 27 August 2012 at 14:44, Christian Sigg wrote:
| Dear Dirk
| Thank you for the reply (and for Rcpp and RInside).
| > | Question: Is the RInside destructor not working properly? I would have expected that both in foo() and bar(), an instance of R is created and then destroyed| immediately, without producing any kind of output.
| > 
| > No, wrong setup. R is single-threaded, and you can have only
| > precisely __ONE__ instance of R in your program. You have two.
| I was not aware that two instances of R are active at the same time. It was my expectation that by instantiating the RInside class as a local variable, at the end of foo() the destructor R.~RInside() would be called and the encapsulated R instance would be terminated, before a new one is instantiated in bar(). 

You are of course correct in a narrow sense and in that same sense I was
wrong in my email. You called two consecutive instances of RInside, not two
paralle ones. That could have worked ....

... but we are still talking R here and the underlying R API for C embedding
leaves (as I recall) a bunch of globals around.  So no two sessions.

| I did take a cursory glance at the implementation of ~RInside() before asking the question, and the function call
| Rf_endEmbeddedR(0);
| gave me that (wrong) impression. I don't know the R implementation internals.

Right. If you REALLY want this to work, you need to work at that end -- ie in
R's implementation.  RInside would then gladly piggyback on your work. Maybe
all it takes is a big cleanup function. I don't know.  

| > Create one in main() and pass references around.
| Is it possible to terminate the RInside instance instead, and later start a new one? I don't think that it is proper to modify main() with the unit testing framework that I'm using (gtest), as the tests are discovered without explicitly calling them from main().

Do you have one long running instance of main()?  That may be an issue for


Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com  

More information about the Rcpp-devel mailing list