I am having a very strange behavior with Rcpp together with libtorch.
I have a file with 2 functions:
#include <torch/torch.h>
#include <Rcpp.h>
// [[Rcpp::export]]
void test_error () {
throw std::runtime_error("hi this is my error");
}
// [[Rcpp::export]]
void test_error2 () {
Rcpp::Rcout << torch::arange(1) << std::endl;
}
When I call test_error()
I get a segfault (g++):
terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc
Aborted (core dumped)
The clang++ error is:
terminate called after throwing an instance of 'std::length_error'
what(): basic_string::_M_create
Aborted (core dumped)
test_error2
works as expected.
This error happens only on Ubuntu Xenial. I tested with Ubuntu Trusty and MacOS and no segfaults.
If I remove the code for test_error2
from the file, I don't have any error, even if I don't remove the #include <torch/torch.h>
line.
Also tested compiling with clang++ and g++. Same error.
I created a smalll repo here with the minimal example I could make.
Does anyone have any idea of what this could be?
Note configure file will download and install libtorch automatically from pytorch's website. So don't install the package if you don't want this.
Turns out that compiling the package with an older version g++
worked fine.
I installed g++-4.9
:
sudo apt-get install g++-4.9
.
Edited the .R/Makevars
to use g++-4.9
:
CXX=g++-4.9
CXX11=g++-4.9
Then recompiled Rcpp and the package.