我正在编写一个包含 C++ 代码的 R 程序包,由无法提供帮助的外部开发人员编写。 C++代码目前似乎存在内存泄漏:R的内存使用量在C++代码运行时不断增加,直到R退出才释放。我的任务是消除这种泄漏。
因为我使用的是 Windows,并通过 R 调用 C++ 代码,所以不清楚如何最好地追踪此泄漏。我狡猾的计划是使用 valgrind在 Travis CI 的 Linux 环境中,但是这个 finds no problems .
追踪内存泄漏的最佳方法是什么?
最佳答案
通过向我的 .travis.yml 添加一个单独的 valgrind 对 R 的调用,我取得了部分成功。
addons:
apt:
packages: valgrind
after_success:
- R -d "valgrind --leak-check=full --track-origins=yes" --vanilla < tests/testthat/valgrind.R
理想情况下,我会运行 tests/testthat.R,但因为 R -d
以交互方式运行,所以我不得不为测试创建一个单独的文件 tests/testthat/valgrind.R:
library(testthat)
# Load the package
library(pkgload)
load_all()
# This may run during build in root working directory,
# then again with R -d from tests wd.
if (dir.exists('testthat')) setwd('testthat/tests')
testFiles <- list.files(pattern = 'test\\-.*\\.R', full.names= TRUE)
# Test files using expect_doppleganger will fail in interactive mode.
# Remove them.
lapply(testFiles[-c(3, 6)], source)
感觉这不是最佳解决方案...但足以满足我的即时需求。
关于c++ - 在 Windows 上诊断构成 R 包一部分的 C++ 代码中的内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58978819/