title: dlclose doesn't really unload shared object, no matter how many times it is called
body: <p>My program uses <code>dlopen</code> to load a shared object and later <code>dlclose</code> to unload it. Sometimes this shared object is loaded once again. I noticed static variables are not re-initialized (something which is crucial to my program) so I added a test (<code>dlopen</code> with <code>RTLD_NOLOAD</code>) after <code>dlclose</code> to see if the library is really unloaded. Sure enough, it was still in memory.</p>
  
  <p>I then tried calling <code>dlclose</code> repeatedly until the library is really unloaded, but what I got was an infinite loop. This is the code I'm using to check if the library was unloaded:</p>
  
  <pre><code>dlclose(handles[name]);
  
  do {
    void *handle = dlopen(filenames[name], RTLD_NOW | RTLD_NOLOAD);
    if (!handle)
      break;
  
    dlclose(handle);
  } while (true);
  </code></pre>
  
  <p>My question is, what are the possible reasons for my shared object not being unloaded after <code>dlclose</code>, given that my <code>dlopen</code> calls are the only places where it is loaded. Can you suggest a course of action to track down the source of the problem? Also, why are repeated calls to <code>dlclose</code> have no effect, they are each decrementing the reference count, aren't they?</p>
  
  <p><strong>EDIT:</strong> Just found out that this happens only when I compile with <strong>gcc</strong>. With <strong>clang</strong>, everything is just fine.</p>