Skip to content

On Windows, exceptions thrown from RCPP modules bypass try(..., silent = TRUE) #1001

@venelin

Description

@venelin

Hello, I am trying to find an answer to the following question:

What is the recommended way of handling exceptions thrown from C++ methods exported using RCPP_MODULEs?

I notice that these exceptions are handled differently on Windows vs Linux/Mac OSX systems. On Linux Mac OSX, it is possible to wrap the call to the C++ method in a try(..., silent = TRUE) and to examine whether the class of the result is a 'try-error'. On Windows, this mechanism seems to fail and the R-process crashes. The package PCMBaseCpp uses Rcpp modules and is compiled using a C++11 compiler with compiler options:


PKG_CXXFLAGS=$(SHLIB_OPENMP_CXXFLAGS) -DARMA_DONT_USE_OPENMP
PKG_LIBS=$(SHLIB_OPENMP_CXXFLAGS) $(LAPACK_LIBS) $(BLAS_LIBS) $(FLIBS)

CXX_STD = CXX11

Below is an example code reproducing the problem (copied from this issue).

install.packages(c("TreeSim", "PCMBase", "PCMBaseCpp"))

library(TreeSim)
library(PCMBase)
library(PCMBaseCpp)

modelBM <- PCMBase::PCM(model = "BM", k = 1)
phyltree<-TreeSim::sim.bd.taxa(n=5,numbsim=1,lambda=1,mu=0)[[1]]
phyltree$root.edge<-NULL
mTraits<-matrix(0,ncol=1,nrow=5)
loglik_PCMBase<-PCMBase::PCMLik(t(mTraits), phyltree, modelBM)
loglik_PCMBase

# This line of code returns an object of class 'try-error' on
# Linux and Mac OSX, but crashes the R process on Windows:
loglik_PCMBaseCpp_PCMLik<-PCMBase::PCMLik(t(mTraits), phyltree, modelBM, metaI = PCMBaseCpp::PCMInfoCpp)

Here's an excerpt of the stack-trace (links pointing to lines of code on github).
try() -->
...
PCMBaseCpp::PCMLmr.PCMInfoCpp -->
...
SPLITT::TraversalSpecification::TraverseTree() -->
...
std::rethrow_exception().

The C++ method TraverseTree is exported via an RCPP_MODULE defined here.

The try(..., silent=TRUE) statement is in the R-package PCMBase here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions