From christian at sigg-iten.ch  Wed Aug 29 16:08:36 2012
From: christian at sigg-iten.ch (Christian Sigg)
Date: Wed, 29 Aug 2012 16:08:36 +0200
Subject: [Rd] Terminating and restarting an embedded R instance possible?
Message-ID: <BA049E17-2711-42DC-8600-A67CA4A4EE7F@sigg-iten.ch>

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. 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


[Rd] Terminating and restarting an embedded R instance possible?
Christian Sigg