My program uses dlopen
to load a shared object and later dlclose
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 (dlopen
with RTLD_NOLOAD
) after dlclose
to see if the library is really unloaded. Sure enough, it was still in memory.
I then tried calling dlclose
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:
dlclose(handles[name]);
do {
void *handle = dlopen(filenames[name], RTLD_NOW | RTLD_NOLOAD);
if (!handle)
break;
dlclose(handle);
} while (true);
My question is, what are the possible reasons for my shared object not being unloaded after dlclose
, given that my dlopen
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 dlclose
have no effect, they are each decrementing the reference count, aren't they?
EDIT: Just found out that this happens only when I compile with gcc. With clang, everything is just fine.