使用 Rcpp 在 Ubuntu Xenial 上抛出 std::runtime_error 时出现段错误
Segfault when throwing std::runtime_error on Ubuntu Xenial with Rcpp
我在使用 Rcpp 和 libtorch 时有一个非常奇怪的行为。
我有一个包含 2 个函数的文件:
#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;
}
当我调用 test_error()
时出现段错误 (g++):
terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc
Aborted (core dumped)
clang++ 错误是:
terminate called after throwing an instance of 'std::length_error'
what(): basic_string::_M_create
Aborted (core dumped)
test_error2
按预期工作。
此错误仅在 Ubuntu Xenial 上发生。我使用 Ubuntu Trusty 和 MacOS 进行了测试,没有出现段错误。
如果我从文件中删除 test_error2
的代码,即使不删除 #include <torch/torch.h>
行,我也不会出现任何错误。
还测试了使用 clang++ 和 g++ 的编译。同样的错误。
我用我能做的最小示例创建了一个小型存储库 here。
有人知道这可能是什么吗?
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.
你能试试替换一下吗
throw std::runtime_error("hi this is my error");
根据我们的文档建议您执行的操作(在 Rcpp 调用的函数中,同样如此)
Rcpp::stop("hi this is my error");
看看会发生什么?
事实证明,使用旧版本 g++
编译软件包工作正常。
我安装了 g++-4.9
:
sudo apt-get install g++-4.9
.
编辑 .R/Makevars
以使用 g++-4.9
:
CXX=g++-4.9
CXX11=g++-4.9
然后重新编译Rcpp和包。
我在使用 Rcpp 和 libtorch 时有一个非常奇怪的行为。
我有一个包含 2 个函数的文件:
#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;
}
当我调用 test_error()
时出现段错误 (g++):
terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc
Aborted (core dumped)
clang++ 错误是:
terminate called after throwing an instance of 'std::length_error'
what(): basic_string::_M_create
Aborted (core dumped)
test_error2
按预期工作。
此错误仅在 Ubuntu Xenial 上发生。我使用 Ubuntu Trusty 和 MacOS 进行了测试,没有出现段错误。
如果我从文件中删除 test_error2
的代码,即使不删除 #include <torch/torch.h>
行,我也不会出现任何错误。
还测试了使用 clang++ 和 g++ 的编译。同样的错误。
我用我能做的最小示例创建了一个小型存储库 here。
有人知道这可能是什么吗?
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.
你能试试替换一下吗
throw std::runtime_error("hi this is my error");
根据我们的文档建议您执行的操作(在 Rcpp 调用的函数中,同样如此)
Rcpp::stop("hi this is my error");
看看会发生什么?
事实证明,使用旧版本 g++
编译软件包工作正常。
我安装了 g++-4.9
:
sudo apt-get install g++-4.9
.
编辑 .R/Makevars
以使用 g++-4.9
:
CXX=g++-4.9
CXX11=g++-4.9
然后重新编译Rcpp和包。