[R] R_unload_[PACKAGENAME] not called

Henrik Alsing Friberg henrik.alsing.friberg at mosek.com
Mon Mar 26 12:24:31 CEST 2012

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

Kind regards,
Henrik Alsing Friberg

More information about the R-help mailing list