[Rcpp-devel] R Session Aborted when calling C++

Dirk Eddelbuettel edd at debian.org
Thu Oct 29 01:15:32 CET 2020


Alexander,

A segmentation fault can take the session down as it is undefined behavior,
yet is almost always the error of the programmers.

We have written the basic accessors for efficiency so they do not check. But
there are checking accessor that throw with a proper message:

  R> Rcpp::cppFunction("void foo(IntegerVector x) { int n = x.size(); Rcpp::Rcout << x.at(n+1) << std::endl; }")
  R> foo(1:3)
  Error in foo(1:3) : Index out of bounds: [index=4; extent=3].
  R>

No segfault, just a clean error message. RcppArmadillo has something similar.

By contrast if you don't check you can get garbage or bad behavior:

  R> Rcpp::cppFunction("void bar(IntegerVector x) { int n = x.size(); Rcpp::Rcout << x[n+1] << std::endl; }")
  R> bar(1:3)
  1358954573
  R>

No crash but not exactly 'right' either.

The other thing you can do is to couple R with gdb to run under the debugger
to get access to your indexing variables. There are some writeups in
different places as it takes two steps---but may be worth it. Here are two
quick hits from StackOverflow:

https://stackoverflow.com/questions/21226337/what-are-productive-ways-to-debug-rcpp-compiled-code-loaded-in-r-on-os-x-maveri
https://stackoverflow.com/questions/11345537/debugging-line-by-line-of-rcpp-generated-dll-under-windows

Dirk

-- 
https://dirk.eddelbuettel.com | @eddelbuettel | edd at debian.org


More information about the Rcpp-devel mailing list