Skip to content

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

4 messages · Renaud Gaujoux, Duncan Murdoch, Brian Ripley

#
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).

  * 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.
  * by curiosity, in what kind of practical situation would a user
want to call detach(..., unload = TRUE)?
  * is there a reason why the hooks are not called on quit?

Thank you.

Bests,
Renaud

###
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"
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
#
On 13-11-06 5:26 AM, Renaud Gaujoux wrote:
No, this is by design.  See ?setHook.
Yes, you can set a finalizer.  See ?reg.finalizer.
They are slow to run, and are usually not needed.  Finalizers handle the 
rare cases where you really do need something to happen.

Duncan Murdoch
#
On 06/11/2013 11:36, Duncan Murdoch wrote:
RODBC is one example.
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.)
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.

  
    
#
Many thanks for all responses and clarification!

It worked great by defining a dummy environment in the package
namespace and calling reg.finalizer to "attach" the cleanup function
to it.

Bests,
Renaud
On 6 November 2013 13:54, Prof Brian Ripley <ripley at stats.ox.ac.uk> wrote: