R软件包在卸载时必须卸载动态库吗? [英] Must R Packages Unload Dynamic Libraries When They Unload?

查看:125
本文介绍了R软件包在卸载时必须卸载动态库吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

摘自Hadley的C最佳成绩 惯例 :

From Hadley's C best practices:

与C ++一样,每当在包中使用C代码时,都应在卸载包时卸载DLL:

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)
}

编写R扩展 甚至没有提到这一点.我可以看到卸载dll的礼貌如何,但是这样做似乎对我加载/卸载/重新加载的软件包造成了一些奇怪的问题(请参见下面的示例).此外,有一些提及表明可能不需要卸载.来自?library.dynam:

请注意,是否可以卸载DLL,然后重新加载同一文件的修订版取决于操作系统:请参阅dyn.unload帮助的值"部分.

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在R-devel中的评论 :

though this shouldn't affect objects that are not modified. Then there is this comment from Brian Ripley in R-devel:

话虽如此,我的经验是,如果您需要再次加载DLL,则卸载DLL通常无济于事(这就是为什么tcltk不会卸载其DLL的原因.)

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库保持加载状态是否可以接受?我希望不必深入研究为什么会发生以下情况(在我开始卸载库之前没有发生过).

So is it acceptable to leave the C libraries loaded? I would prefer not to have to dig into why stuff like the below is happening (did not happen before I started unloading libraries).

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正常.

The warnings are all related to alike functions. alike did not use to unload its dynamic libraries, and the above errors did not happen. After I implemented unloading the errors started happening. Note that data.table 1.9.2 did not unload its DLLs, though other packages that also don't unload DLLs didn't cause this problems. data.table 1.9.4 works fine.

推荐答案

通常,卸载DLL是一个好主意.它拥有的资源将被完全释放,并且重新加载不会成为问题.

Normally unloading a DLL would be a good idea. The resources it owns, would be completely freed, and re-loading would not be an issue.

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

In R, there is the complication of the R environment, because even if a DLL is unloaded, there may be some knowledge left behind in the R runtime. In this case, the result may be that the re-loaded DLL library does not share the same inferred state as the R variables which are intended to understand the DLL state, and thus errors occur.

我认为可以安全地卸载R包(DLL和R代码),但是除非您发现资源使用特别繁重,否则将DLL装载起来会更容易.

I think it would be possible for an R package (DLL and R code) to be safely unloaded, but it would be easier for you to leave the DLLs loaded, unless you find particularly heavy resource usage.

这篇关于R软件包在卸载时必须卸载动态库吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆