[Rcpp-devel] Segfault when attributes are not used

Matteo Fasiolo matteo.fasiolo at gmail.com
Sun Aug 10 17:14:47 CEST 2014


Thanks for your reply. Ok so this is the bug I remembered, in fact it
happens all the time under gctorture(TRUE),
while inverting the order of:

RNGScope scope;

and

NumericMatrix out(10, 10);

makes it disappear.

At the moment all my packages use the form:

RcppExport SEXP myFun(){

RNGScope scope;
...

}

should I then switch to the form:

\\ [[Rcpp::export]]
RcppExport SEXP myFun(){

\\ Not add RNGScope
...

}

? Maybe in the short term I'll just use the swapping trick! Strangely no
error occurs if a NumericVector is returned.

Thanks,

Matteo


On Sun, Aug 10, 2014 at 3:59 PM, JJ Allaire <jj at rstudio.com> wrote:

> Yes, there is a subtle bug in RNGScope which can cause this. The good news
> is that you don't need RNGScope (since it's added, along with a workaround
> for the bug, by Rcpp::export)
>
>
> On Aug 10, 2014, at 10:37 AM, Matteo Fasiolo <matteo.fasiolo at gmail.com>
> wrote:
>
> Dear Rcpp developer,
>
>  I have experienced intermittent crashes of some of my
> Rcpp function and I managed to reduce it to the following.
>
> I build a package using Rcpp.package.skeleton(), I delete
> all the files in the R and src folder, and I include in the src
> folder 2 files:
>
> ####### exper.cpp
>
> #include <Rcpp.h>
> using namespace Rcpp;
>
> // [[Rcpp::export]]
> NumericMatrix exper( )
> {
>      RNGScope scope;
>
>       NumericMatrix out(10, 10);
>
>       return out;
>
> }
>
> ######### exper2.cpp
>
> #include <Rcpp.h>
> using namespace Rcpp;
>
> RcppExport SEXP exper2( )
> {
>       RNGScope scope;
>
>       NumericMatrix out(10, 10);
>
>       return out;
>
> }
>
> The first version uses // [[Rcpp::export]] the second RcppExport.
> I then call compileAttributes(".") inside the package folder.
>
> Then I run:
>
> #### test.R
>
> library(anRpackage)
> set.seed(4341)
> for(ii in 1:500000){
>
>   a <- .Call( "anRpackage_exper",  PACKAGE = "anRpackage" )
>
>   matrix(a, 1, 100)
> }
>
> set.seed(4341)
> for(ii in 1:500000){
>
>   a <- .Call( "exper2",  PACKAGE = "anRpackage" )
>
>   matrix(a, 1, 100)
> }
>
> exper2 crashes with:
>
>  *** caught segfault ***
> address (nil), cause 'unknown'
>
>  Am I doing something wrong?
>
> Removing the line "RNGScope scope;" solves the problem.
>
> Maybe there is a relation with the problem found in this old discussion
> <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/2013-May/005815.html>
> ?
>
> Many thanks for you help.
>
> Matteo
>
> _______________________________________________
> Rcpp-devel mailing list
> Rcpp-devel at lists.r-forge.r-project.org
> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20140810/a5240432/attachment.html>


More information about the Rcpp-devel mailing list