[R] R_unload_[PACKAGENAME] not called

Duncan Murdoch murdoch.duncan at gmail.com
Mon Mar 26 16:21:33 CEST 2012


On 26/03/2012 6:24 AM, Henrik Alsing Friberg wrote:
> Hi R-help users..
>
> I mistakenly posted a now rejected bug report
> (https://bugs.r-project.org/bugzilla3/show_bug.cgi?id=14855), but the
> subject still bugs me..!
>
> If I have a package called MyPackage with C code inside, then library
> or require(MyPackage) will call loadNamespace() which will look for
> and call:
> void R_init_MyPackage(DllInfo *info)
>
> At the end of the R session, the corresponding unload function is
> however *not* called.
> void R_unload_MyPackage(DllInfo *info)
>
> I use the two functions to call R_cholmod_start(CHM_CM) and
> cholmod_finish(CHM_CM) respectively, and I think this might leak
> memory if the latter is not called. Firstly, why is one called without
> the package maker having to think about it, while the other is not?
> Secondly, how do I make sure that the unload function is called --
> using either .Last.lib or .onUnload to call unloadNamespace() seems
> like a bad idea, as unloadNamespace() is documented to call back to
> these hooks (the call cycle would have to by broken by other means)?

You would normally use reg.finalizer() for this, or the C level 
equivalents (see the section on external pointers in Writing R Extensions).

Duncan Murdoch



More information about the R-help mailing list