Search code examples
rpackagercpprcpparmadillo

Rcpp functions cause crash in package


I have a package, https://github.com/tfrostig/RSEE, which includes a few (3) RcppArmadillo functions. The package works well on other computer. When I build the package no errors appear, but whenever I call any of the RCPP functions it causes R to crash.

When I try to use my Unit Testing, I obtain the error: "Exited with status -1073741819" .

If I use Rcpp::sourceCpp() and then call the functions, everything works well. Other packages with Rcpp functions work well.

For example:

`// [[Rcpp::depends(RcppArmadillo)]]
#include <RcppArmadillo.h>
using namespace Rcpp;
using namespace arma;


// [[Rcpp::export]]
arma::mat localRegression(arma::mat weightmat, arma::mat modelmat, arma::vec xtemp) {
  return inv(modelmat.t() * weightmat * modelmat) * modelmat.t() * weightmat * xtemp;
}

Using RSEE:::localRegression will cause it to crash. If I load the source code using sourceCpp and then call localRegression it works ok.

What can cause this type of problem?

The session info is: 
R version 4.0.3 (2020-10-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19042)

Matrix products: default

locale:
[1] LC_COLLATE=English_Israel.1252  LC_CTYPE=English_Israel.1252    LC_MONETARY=English_Israel.1252
[4] LC_NUMERIC=C                    LC_TIME=English_Israel.1252    

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

other attached packages:
[1] RSEE_0.1.0

loaded via a namespace (and not attached):
[1] compiler_4.0.3 tools_4.0.3    Rcpp_1.0.6    

Solution

  • Taking a look at your package, I am assuming that the error and crash comes from arma::mat iterLowess(..., double epsmed = 10^(-6)) { in src/RCPP_LOWESS.cpp. Note that ^ is not a power operation in Cpp, but instead a byte XOR operation. Furthermore 10 is an integer while 10.0 is a double, so while the compiler "should" do automatic conversion it might simply fail.

    Try for example

    library(Rcpp)
    f <- cppFunction('double powww(double x = 10^(-6)){
                     double y = x^2;
                     return y;}')
    

    and you'll notice this throws an error.

    It is almost impossible to give you an exact answer, as we have very limited information in the question, but since you mention it crashes immediately upon calling the function (I'm assuming it does so in the debugging state as well) we should be looking at the function definition for our error.