(GNU) `dlopen` a dynamic library multiple times
- The same file handle is returned multiple times.
- If the path provided to
dlopendiffers (even when the absolute path is the same), different file handles are returned.
- If the path provided to
- A reference count is maintained by the dl library,
so a dynamic library needs to be closed as many times using
dlcloseas it has been opened using dlopen.- The dynamic library is unloaded only when the reference count drops to 0 (+ no other loaded libraries use its symbol).
dlopenwithRTLD_NOLOADflag can be used todlopenwithRTLD_NOLOADreturnsNULLif the target library is not resident on memory, or the handle of the library if it is resident. This can be used to:- avoid
dlopenfrom opening the same library more than once - check whether the reference count has dropped to 0. E.g.:
// NOTE: Below code has not been tested while ((void *handle = dlopen(TARGET_LIB_PATH, RTLD_NOLOAD)) != NULL) { dlclose(handle); }
- avoid
References:
Written on March 9, 2021
