The POSIX standard does not actually require dlclose to unload a library from the address space:
Although the dlclose () operation from the address space is not required to remove structures, none of the implementations are allowed to do this.
Source: Open Group Issue 6 Core Specifications
This means that an invalid dlclose descriptor descriptor is not required at all.
Sometimes unloading is also delayed by the system, it simply puts the library as βdeletedβ and actually performs this operation at a later time (for efficiency or because it simply will not be possible to perform this operation right now). However, if you call dlopen again before it has ever been executed, the flag is cleared and the reloaded library is reused.
In some cases, the system knows for sure that some library symbols are still in use, in which case it will not unload it from the address space in order to avoid dangling pointers. In some cases, the system does not know for sure that they are used, but it also cannot say for sure that it is not, it is better to be safe than sorry, it simply will never delete this library from memory in this case.
There are other more obscure cases depending on the type of operating system, and also often on the version. For example. A common problem with Linux is that if you created a library that uses the STB_GNU_UNIQUE characters, this library is marked as "not unloaded" and therefore simply will not be unloaded. See here , here ( DF_1_NODELETE means not DF_1_NODELETE ) and. Thus, it may also depend on what symbols or type of symbol the compiler generates. Try running readelf -Ws in your library and find objects with UNIQUE tags.
In general, you cannot rely on dlclose to work as you might expect. In practice, I saw that he "failed" more often than "succeeded" in the last ten years (well, he never lost, he just did not unload the library from memory, but he worked in accordance with the standards).