From simon.urbanek at r-project.org  Thu Aug 30 17:14:59 2012
From: simon.urbanek at r-project.org (Simon Urbanek)
Date: Thu, 30 Aug 2012 11:14:59 -0400
Subject: [Rd] Terminating and restarting an embedded R instance possible?
In-Reply-To: <BA049E17-2711-42DC-8600-A67CA4A4EE7F@sigg-iten.ch>
References: <BA049E17-2711-42DC-8600-A67CA4A4EE7F@sigg-iten.ch>
Message-ID: <09421E43-1529-4061-8065-FB17219D1960@r-project.org>


On Aug 29, 2012, at 10:08 AM, Christian Sigg <christian at sigg-iten.ch> wrote:

> I am developing R functions that interface with C++ code from the OpenCV library. During  development and unit testing, I embed an R instance to prepare the environment in which the function encapsulating OpenCV code is called.
> 
> Running the unit testing suite results in starting (and later terminating) an embedded R instance for each test. This produces a series of error messages when Rf_initEmbeddedR is called for the second time.

To my best knowledge you cannot init R more than once in the same process. There are many global variables that rely on being initialized by the data segment. Supporting re-init of R would be a considerable effort as many of those are static so there is no simple way to re-set them.

Cheers,
Simon



> This error is reproduced by running
> 
> #include <Rembedded.h>
> 
> int main(int argc, char **argv) {
> 
>    Rf_initEmbeddedR(argc, argv);
>    Rf_endEmbeddedR(0);
> 
>    Rf_initEmbeddedR(argc, argv);
>    Rf_endEmbeddedR(0);
> 
>    return 0;
> }
> 
> which outputs the following two lines
> 
> Error: bad target context--should NEVER happen;
> please bug.report() [R_run_onexits]
> 
> a great number of times (this is on OS X 10.7 with Xcode 4.4.1, version 4.4.0.0.1.124936715 of the command line tools and R 2.15.1).
> 
> Debugging the above program indicates that the error happens in installFunTab(). Not knowing R internals, I can only guess that state from the first embedded instance leaks into the second instance.
> 
> Soeren Sonnenburg asked a similar question on March 5th 2009 but there was no reply. He also mentioned a bug report
> 
> http://bugs.r-project.org/cgi-bin/R/trashcan?id=12644;user=guest;selectid=12644
> 
> but that link no longer works and bug # 12644 is not in the current database.
> 
> Can the above mentioned code be made to work?
> 
> Thank you,
> Christian
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
> 
> 



[Rd] Terminating and restarting an embedded R instance possible?
Simon Urbanek