c++ - 使用 Rcpp 在 Ubuntu Xenial 上抛出 std::runtime_error 时出现段错误

标签 c++ r rcpp libtorch

我在使用 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");

看看会发生什么?

关于c++ - 使用 Rcpp 在 Ubuntu Xenial 上抛出 std::runtime_error 时出现段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54492667/

相关文章:

c++ - 在 Rcpp 中调试运行时错误

c++ - QT 自定义 TreeView

c++ - 如何使用 IDirectManipulationViewport::SetViewportRect?

r - 在 OS X 上分析 Rcpp 代码

r - 检查 R 中变量值 (v) 的前一个/连续数字

r - 向量化更改矩阵列的 for 循环

r - 在 Linux 集群 : compiler issues? 上安装 R `forecast` 软件包

c++ - Kext OSDynamicCast 在 OSObject::free 期间失败

c++ - 无法修改类中的对象 vector

r - model.frame.default : variable lengths differ 中的错误