Should R packages unload dynamic libraries when they are unloaded?

From Hadley C best practices :

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

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

Writing R-extensions , on the other hand, doesn't even mention it. I see how it would be politely to unload the DLL, but it seems to me that there are some strange problems with loaded / unloaded / reloaded packages (see the example below). In addition, there are some references that suggest that unloading is not required. From ?library.dynam :

Please note that regardless of whether it is possible to unload the DLL and then reload the corrected version of the same file, it depends on the OS: see the section β€œHelp value for dyn.unload.

although this should not affect objects that are not modified. Then there is this comment from Brian Ripley in R-devel :

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

So is it acceptable to leave the loaded C libraries? I would prefer not to delve into what is happening, as it happens below (it was not before I started unloading the 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' 

All warnings are related to alike features. alike did not use to upload its dynamic libraries, and the above errors did not occur. After I unloaded, the errors started. Please note that data.table 1.9.2 does not unload its DLLs, although other packages that also do not unload DLLs did not cause such problems. data.table 1.9.4 works fine.

+42
c r dll packages
Nov 01 '14 at 18:50
source share
1 answer

Normal DLL unloading would be a good idea. The resources he owns will be completely freed, and rebooting will not be a problem.

In R there is a complication of the R environment, because even if the DLL is unloaded, some knowledge may remain in the R environment. In this case, the result may be that the reloaded DLL does not use the same assumed state as R variables, which are intended to understand the state of the DLL, and therefore errors occur.

I think that it would be safe to unload the R package (DLL and R-code), but it would be easier for you to leave the loaded DLLs if you do not find much use of resources.

+2
May 7 '17 at 16:06
source share
β€” -



All Articles