c - R包卸载的时候一定要卸载动态库吗?

标签 c r dll package

来自 Hadley 的 C 最佳 practices :

Like with C++, whenever you use C code in your package, you should unload the DLL when the package is unloaded:

.onUnload <- function (libpath) {
  library.dynam.unload("mypackage", libpath)
}

Writing R Extensions 另一方面甚至没有提到这一点。我可以看到卸载 dll 是多么礼貌,但这样做似乎会给我带来一些奇怪的问题,这些问题对我来说是加载/卸载/重新加载的包(请参见下面的示例)。此外,还有一些提及表明可能不需要卸载。来自 ?library.dynam:

Note that whether or not it is possible to unload a DLL and then reload a revised version of the same file is OS-dependent: see the ‘Value’ section of the help for dyn.unload.

虽然这不应该影响未修改的对象。然后是来自 Brian Ripley in R-devel的评论:

Having said all that, my experience is that unloading the DLL often does not help if you need to load it again (and that is why e.g. tcltk does not unload its DLL).

那么让 C 库加载是可以接受的吗?我宁愿不必深入研究为什么会发生下面这样的事情(在我开始卸载库之前没有发生)。

R version 3.1.1 (2014-07-10)
Platform: x86_64-apple-darwin13.1.0 (64-bit)

> library(alike)       # install_github("brodieg/alike", ref="fdaa578e"), if you're curious
> library(data.table)
data.table 1.9.2  For help type: help("data.table")
> detach("package:data.table", unload=T)
> detach("package:alike", unload=T)
> library(alike)
> library(data.table)
Error : .onLoad failed in loadNamespace() for 'data.table', details:
  call: address(x)
  error: object 'Caddress' not found
In addition: Warning messages:
1: In FUN(X[[9L]], ...) :
  failed to assign RegisteredNativeSymbol for alike to alike since alike is already defined in the ‘data.table’ namespace
2: In FUN(X[[9L]], ...) :
  failed to assign RegisteredNativeSymbol for typeof to typeof since typeof is already defined in the ‘data.table’ namespace
3: In FUN(X[[9L]], ...) :
  failed to assign RegisteredNativeSymbol for type_alike to type_alike since type_alike is already defined in the ‘data.table’ namespace
Error: package or namespace load failed for ‘data.table’

警告都与alike 函数有关。 alike 没有使用卸载它的动态库,也就没有出现上面的错误。在我实现卸载后,错误开始发生。请注意,data.table 1.9.2 没有卸载它的 DLL,但也没有卸载 DLL 的其他包不会导致此问题。 data.table 1.9.4 工作正常。

最佳答案

通常卸载 DLL 是个好主意。它拥有的资源将被完全释放,重新加载将不是问题。

在 R 中,存在 R 环境的复杂性,因为即使卸载了 DLL,R 运行时也可能会留下一些知识。在这种情况下,结果可能是重新加载的 DLL 库与旨在了解 DLL 状态的 R 变量共享相同的推断状态,从而发生错误。

我认为可以安全地卸载 R 包(DLL 和 R 代码),但让 DLL 保持加载状态会更容易,除非您发现资源使用量特别大。

关于c - R包卸载的时候一定要卸载动态库吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26691878/

相关文章:

c - 为什么 root 的值在 main 函数中打印为 0?

r - 使用 R 的 Bing Ads API 报告数据 - 获取 ReportRequestId

r - R中的重叠矩阵

security - 如何在Delphi中调用WinAPI函数SetDllDirectory()?

c - 在 C 语言中使用 GEOS API 函数返回段错误

c - 从 C 中的 float 获​​取指数

c# - 如何知道 pdb 文件在哪里寻找源代码

c++ - 链接器 - 模块机器类型冲突

c - 什么时候可以使用 "void()",这样做有什么好处

r - 按类别和计数的直方图