[Rcpp-devel] (Very) rare segfault

Romain Francois romain at r-enthusiasts.com
Thu Aug 21 11:34:23 CEST 2014


Le 20 août 2014 à 20:57, Gregor Kastner <gregor.kastner at wu.ac.at> a écrit :

> JJ> Yes, RNGScope isn't safe by itself. You can use it via attributes (and we
> JJ> make sure to use it correctly) or you could use this pattern e.g. if you
> JJ> are planning to return a NumericVector:
> JJ> 
> JJ> NumericVector vec(20);   // or whatever size, the important thing is
> JJ> allocating the return SEXP before RNGScope
> JJ> 
> JJ> RNGScope rngScope;
> JJ> 
> JJ> // do your thing
> JJ> 
> JJ> 
> JJ> return vec;

JJ, care to explain this ? I don't follow. 

> OK thanks. That's definitely worth knowing. Might be a cause for trouble for
> others too, especially since misuse (i.e. other ordering) doesn't seem to show
> any effects in many cases.
> 
> DE> RNGScope is not for for seed setting, it is for RNG 'state sanity'. See
> DE> Section 6.3 of 'Writing R Extensions' -- worst case you can just call
> DE> GetRNGstate() and PutRNGstate() by hand.  Though we never had to
> DE> recommend that yet.
> 
> Yep, sorry for the misuse of language. And I do understand going back to
> GetRNGstate() and PutRNGstate() is a bit old school; but I can definitely
> confirm that it seems to be safer than resorting do the
> constructor/destuctor magic.

Compiler/Destructor is not magic. It is determinism. 

That's like feature #1 of C++. 

And BTW it is definitely safer than any manual handling.  



More information about the Rcpp-devel mailing list