Search code examples
rcpp

Rcpp::stop() crashes R under g++


Here's a simple test function that calls Rcpp::stop()

#include <Rcpp.h>

NumericVector dostop()
{
  Rcpp::stop("foo");
  NumericVector x(1);

  return x;
}

With clang on OS X, this performs as expected:

> library(Rcpp)
> sourceCpp('stop.cpp')
> dostop()
Error in dostop() : foo
> 

However, with g++ on Linux, this function crashes R:

> library(Rcpp)
> sourceCpp('stop.cpp')
> dostop()
*** glibc detected *** /share/apps/R/3.3.3/lib64/R/bin/exec/R: free(): invalid pointer: 0x0000000001c29ff8 ***
======= Backtrace: =========
/lib64/libc.so.6[0x3407c75f4e]
/lib64/libc.so.6[0x3407c78cad]
/tmp/RtmpiUT8Xb/sourceCpp-x86_64-pc-linux-gnu-1.0.1/sourcecpp_7dc862cd8f07/sourceCpp_2.so(_Z31exception_to_condition_templateIN4Rcpp9exceptionEEP7SEXPRECRKT_b+0x1da)[0x7f6fab831287]
/tmp/RtmpiUT8Xb/sourceCpp-x86_64-pc-linux-gnu-1.0.1/sourcecpp_7dc862cd8f07/sourceCpp_2.so(_Z29rcpp_exception_to_r_conditionRKN4Rcpp9exceptionE+0x29)[0x7f6fab83073d]
/tmp/RtmpiUT8Xb/sourceCpp-x86_64-pc-linux-gnu-1.0.1/sourcecpp_7dc862cd8f07/sourceCpp_2.so(sourceCpp_1_dostop+0x27d)[0x7f6fab82dd02]
/share/apps/R/3.3.3/lib64/R/lib/libR.so(+0xdc4fe)[0x7f6fb4ea04fe]
(and many more lines of similar)

Here is the relevant sessionInfo:

> sessionInfo()
R version 3.3.3 (2017-03-06)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Red Hat Enterprise Linux Workstation release 6.6 (Santiago)

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] Rcpp_1.0.1

Question 1: Should am I using Rcpp::stop() correctly? That is, should I expect to be able to call it and get an error message at the console like in the OS X case?

Question 2: If so, is there any workaround for the decidedly suboptimal behavior that I'm seeing on Linux/g++?


Solution

  • Brief answers:

    Question 1

    You are using Rcpp::stop() correctly. But your system is a few years, and several R releases behind, and we made several dozen (!!) Rcpp releases since it was current. That combination of stone-old R and compiler with current Rcpp is rare.

    Question 2

    Upgrade to current versions, or stick with the ancient ones we made when those R and g++ versions rules. Maybe try an Rcpp version 0.12.* or 0.13.*.

    Example

    On current OS and compiler versions:

    > Rcpp::cppFunction('double doStop(NumericVector x) { stop("foo"); return x[1]; }')
    > doStop(c(1.2, 3.4))
    Error in doStop(c(1.2, 3.4)) : foo
    > 
    > sessionInfo()
    R version 3.6.0 (2019-04-26)
    Platform: x86_64-pc-linux-gnu (64-bit)
    Running under: Ubuntu 18.10
    
    Matrix products: default
    BLAS:   /usr/lib/x86_64-linux-gnu/openblas/libblas.so.3
    LAPACK: /usr/lib/x86_64-linux-gnu/libopenblasp-r0.3.3.so
    
    locale:
     [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
     [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
     [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
     [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
     [9] LC_ADDRESS=C               LC_TELEPHONE=C            
    [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       
    
    attached base packages:
    [1] stats     graphics  grDevices utils     datasets  methods   base     
    
    loaded via a namespace (and not attached):
    [1] compiler_3.6.0 tools_3.6.0    Rcpp_1.0.1    
    >