title: Must R Packages Unload Dynamic Libraries When They Unload?
body: <p>From Hadley's C best <strong><a href="http://r-pkgs.had.co.nz/src.html#c-best-practices" rel="noreferrer">practices</a></strong>:</p>
  
  <blockquote>
    <p>Like with C++, whenever you use C code in your package, you should unload the DLL when the package is unloaded:</p>
  </blockquote>
  
  <pre><code>.onUnload &lt;- function (libpath) {
    library.dynam.unload("mypackage", libpath)
  }
  </code></pre>
  
  <p><strong><a href="http://cran.r-project.org/doc/manuals/r-release/R-exts.html" rel="noreferrer">Writing R Extensions</a></strong> on the other hand doesn't even mention this.  I can see how it would be polite to unload the dlls, but doing so seems to cause some weird problems for me with packages that are loaded/unloaded/reloaded (see example further down).  Additionally, there are some mentions that suggest maybe unloading isn't required.  From <code>?library.dynam</code>:</p>
  
  <blockquote>
    <p>Note that whether or not it is possible to unload a DLL and then reload a revised version of the same file is OS-dependent: see the ‘Value’ section of the help for dyn.unload.</p>
  </blockquote>
  
  <p>though this shouldn't affect objects that are not modified.  Then there is this comment from <strong><a href="https://stat.ethz.ch/pipermail/r-devel/2010-November/059062.html" rel="noreferrer">Brian Ripley in R-devel</a></strong>:</p>
  
  <blockquote>
    <p>Having said all that, my experience is that unloading the DLL often does not help if you need to load it again (and that is why e.g. tcltk does not unload its DLL).</p>
  </blockquote>
  
  <p>So is it acceptable to leave the C libraries loaded?  I would prefer not to have to dig into why stuff like the below is happening (did not happen before I started unloading libraries).</p>
  
  <pre><code>R version 3.1.1 (2014-07-10)
  Platform: x86_64-apple-darwin13.1.0 (64-bit)
  
  &gt; library(alike)       # install_github("brodieg/alike", ref="fdaa578e"), if you're curious
  &gt; library(data.table)
  data.table 1.9.2  For help type: help("data.table")
  &gt; detach("package:data.table", unload=T)
  &gt; detach("package:alike", unload=T)
  &gt; library(alike)
  &gt; 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’
  </code></pre>
  
  <p>The warnings are all related to <code>alike</code> functions.  <code>alike</code> did not use to unload its dynamic libraries, and the above errors did not happen.  After I implemented unloading the errors started happening.  Note that <code>data.table 1.9.2</code> did not unload its DLLs, though other packages that also don't unload DLLs didn't cause this problems.  <code>data.table 1.9.4</code> works fine.</p>