[Rd] How to do package cleanup: hooks .onUnload, R_unload_mylib, .onDetach are not called on quit

Prof Brian Ripley ripley at stats.ox.ac.uk
Wed Nov 6 12:54:57 CET 2013


On 06/11/2013 11:36, Duncan Murdoch wrote:
> On 13-11-06 5:26 AM, Renaud Gaujoux wrote:
>> Hi,
>>
>> it seems that the package hooks .onLoad and its C++ pendant
>> R_unload_mylib are actually not called when R quits, but only when
>> explicitly calling detach('package:mylib', unload = TRUE).
>> Maybe this is platform specific, I'm on Ubuntu 13.10 - R 3.0.2 (see
>> below).
>
> No, this is by design.  See ?setHook.
>
>>
>>    * is there a mechanism that a package can use to effectively do some
>> cleanup on standard exit, such as calling cleaning up routines of a
>> loaded third-party library? I tried .onDetach but it did not work
>> either.
>
> Yes, you can set a finalizer.  See ?reg.finalizer.

RODBC is one example.
>
>
>
>>    * by curiosity, in what kind of practical situation would a user
>> want to call detach(..., unload = TRUE)?

An example is when you want a different version of a package.  (That 
might now work and probably will not if the package does not unload its 
DLL.)

>>    * is there a reason why the hooks are not called on quit?
>
> They are slow to run, and are usually not needed.  Finalizers handle the
> rare cases where you really do need something to happen.

Or to put it another way, the OS will do most of the unloading when it 
terminates the process, more efficiently than the process itself can. 
That includes 'unloading' DLLs and freeing memory.

>
> Duncan Murdoch
>
>>
>> Thank you.
>>
>> Bests,
>> Renaud
>>
>> ###
>>> Sys.info()
>>
>>         sysname                                      release
>>                            version
>>                                       "Linux"
>> "3.11.0-12-generic" "#19-Ubuntu SMP Wed Oct 9 16:20:46 UTC 2013"
>>                                      nodename
>>             machine                                        login
>>                                       "XXXXX"
>>            "x86_64"                                     "renaud"
>>                                          user
>>      effective_user
>>                                      "renaud"
>>            "renaud"
>>> sessionInfo()
>> R version 3.0.2 (2013-09-25)
>> Platform: x86_64-pc-linux-gnu (64-bit)
>>
>> locale:
>>   [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C
>> LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8
>> LC_MONETARY=en_US.UTF-8
>>   [6] LC_MESSAGES=en_US.UTF-8    LC_PAPER=en_US.UTF-8       LC_NAME=C
>>                 LC_ADDRESS=C               LC_TELEPHONE=C
>> [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
>>
>> attached base packages:
>> [1] stats     graphics  grDevices utils     datasets  methods   base
>>>
>>
>> ______________________________________________
>> R-devel at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-devel
>>
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel


-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595



More information about the R-devel mailing list