In general, once an Rcpp API gives you a SEXP, it is unprotected and
the onus is on your to manage its protection.
In your code:
SEXP intSexp = call1.eval();
Language call2("levels",facSexp);
SEXP levelsSexp = call2.eval();
vals = as< std::vector<int> >(intSexp); //here be errors
with gctorture
Those SEXPs are unprotected, so would be eligible for garbage collection.
You could instead use Shield<SEXP> or RObject to act as thin wrappers
over a SEXP which needs protection from the garbage collector.
Best,
Kevin
On Wed, Jan 9, 2019 at 4:23 PM Fellows, Ian <[email protected]> wrote:
>
>
> Thank you Bill,
>
> I have tried gctorture, and low and behold I got an error in some of my code
> (Yay!). I was able to pin the gctorture error to something small and
> reproducible. It turns out in a one place I created temporary SEXP variables
> (see the example below), and I guess these were not protected automatically
> by Rcpp. I’ve changed these to RObject objects and things seem to work fine,
> though I’ll have to do a bunch more builds to be sure.
>
> This has gotten me a little worried about other places in the code where
> SEXPs pop up. I’ve now limited the use of SEXPs to function parameters and
> return values. This is mostly for transport of objects to and from R. Let’s
> say I have a method like
>
> SEXP afunc(){
> List lis;
> // add things to list
> return lis;
> }
>
> My understanding is that afunc safe to use in a module. Is it also safe to
> call from other C++ code, or is lis open to garbage collection?
>
>
>
> Thanks,
> Ian
>
>
>
> ---------
>
> library(inline)
> library(Rcpp)
> inc <- '
> using namespace Rcpp;
> class TestObj {
> public:
> TestObj(int i){}
>
> void set(SEXP robj){
> try{
> Language call("as.factor",robj);
> RObject facSexp = call.eval();
> Language call1("as.integer",facSexp);
> SEXP intSexp = call1.eval();
> Language call2("levels",facSexp);
> SEXP levelsSexp = call2.eval();
> vals = as< std::vector<int> >(intSexp); //here be errors with
> gctorture
> } catch(std::exception &ex) {
> forward_exception_to_r(ex);
> } catch(...){
> ::Rf_error("error, invalid object addDiscreteVariableR");
> }
>
> }
> std::vector<int> vals;
> };
> RCPP_MODULE(mod) {
> class_<TestObj>( "TestObj" )
> .constructor<int>()
> .method( "set", &TestObj::set )
> ;
> }
> '
> fx <- cxxfunction(signature(), plugin="Rcpp", include=inc)
> mod <- Module("mod", getDynLib(fx))
> obj <- new(mod$TestObj, 1L)
> vals <- 1:4
> gc()
> gctorture(T)
> obj$set(vals)
> # Error in obj$set(vals) :
> # Not compatible with requested type: [type=list; target=integer].
>
> --------
>
>
>
>
>
> On January 8, 2019 at 2:35:30 PM, William Dunlap
> ([email protected](mailto:[email protected])) wrote:
>
> > > --- re-building ‘lolog-ergm.Rmd’ using rmarkdown
> > > Quitting from lines 69-75 (lolog-ergm.Rmd)
> > > Error: processing vignette 'lolog-ergm.Rmd' failed with diagnostics:
> > > unimplemented type (28) in 'duplicate'
> >
> > I don't believe there is a type (*SXP) 28 in R. This could be due to memory
> > misuse. Have you tried running the R code in lolog-ergm.Rmd (and your unit
> > tests) in R
> > after setting gctorture(TRUE)?
> >
> > Bill Dunlap
> > TIBCO Software
> > wdunlap tibco.com(http://tibco.com)
> >
> > On Tue, Jan 8, 2019 at 1:31 PM Fellows, Ian wrote:
> > >
> > > Hi All,
> > >
> > > I have a package ( https://github.com/statnet/lolog |
> > > https://cran.r-project.org/web/packages/lolog/index.html ) that makes
> > > extensive use of Rcpp modules and passes module objects up and down
> > > between R and C++ regularly. I’ve used the package extensively in large
> > > simulation studies, and analyses where it has been stable.
> > >
> > > However, I have become aware of an intermittent failure to re-build the
> > > vignettes during R CMD check. I’ve only ever encountered the issue during
> > > check. Every once in a while I get WARNINGS like the following:
> > >
> > >
> > > -------
> > >
> > > * checking re-building of vignette outputs ... WARNING
> > > Error(s) in re-building vignettes:
> > > ...
> > > --- re-building ‘lolog-ergm.Rmd’ using rmarkdown
> > > Quitting from lines 69-75 (lolog-ergm.Rmd)
> > > Error: processing vignette 'lolog-ergm.Rmd' failed with diagnostics:
> > > unimplemented type (28) in 'duplicate'
> > >
> > > --- failed re-building ‘lolog-ergm.Rmd’
> > >
> > > --- re-building ‘lolog-introduction.Rmd’ using rmarkdown
> > > --- finished re-building ‘lolog-introduction.Rmd’
> > >
> > > SUMMARY: processing the following file failed:
> > > ‘lolog-ergm.Rmd’
> > >
> > > Error: Vignette re-building failed.
> > > Execution halted
> > >
> > > -------
> > >
> > > or
> > >
> > > -------
> > >
> > > * checking re-building of vignette outputs ... [336s/452s] WARNING
> > > Error(s) in re-building vignettes:
> > > ...
> > > --- re-building ‘lolog-ergm.Rmd’ using rmarkdown
> > > --- finished re-building ‘lolog-ergm.Rmd’
> > >
> > > --- re-building ‘lolog-introduction.Rmd’ using rmarkdown
> > > Quitting from lines 355-381 (lolog-introduction.Rmd)
> > > Error: processing vignette 'lolog-introduction.Rmd' failed with
> > > diagnostics:
> > > cannot coerce type 'environment' to vector of type 'any'
> > > --- failed re-building ‘lolog-introduction.Rmd’
> > >
> > > SUMMARY: processing the following file failed:
> > > ‘lolog-introduction.Rmd’
> > >
> > > Error: Vignette re-building failed.
> > > Execution halted
> > >
> > > -------
> > >
> > >
> > > I’ve attempted to isolate the problem to something of a smaller but have
> > > thus far been unsuccessful. Since this only happens occasionally, I
> > > thought it might have something to do with how I’m passing objects up and
> > > down ( see for example:
> > > https://github.com/statnet/lolog/blob/master/inst/include/Model.h#L89 and
> > > https://github.com/statnet/lolog/blob/master/inst/include/util.h#L29 ),
> > > but that is just a shot in the dark.
> > >
> > > Does anyone have any insight as to how i might be able to better debug
> > > this or intuitions about what could be the cause? Is it possible to get a
> > > stack trace when rebuilding the vignettes?
> > >
> > > My apologies for the lack of a simple reproducible example.
> > >
> > > Sincerely,
> > > Ian Fellows
> > >
> > >
> > >
> > >
> > >
> > >
> > > _______________________________________________
> > > Rcpp-devel mailing list
> > > [email protected](mailto:[email protected])
> > > https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
> _______________________________________________
> Rcpp-devel mailing list
> [email protected]
> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
_______________________________________________
Rcpp-devel mailing list
[email protected]
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel