If dlclose reduces the reference count to zero and the library is actually unloaded, any future reloading of the library should reset all variables with static storage duration in the library to their original values.
However, if the library was opened more than once, all but the final call to dlclose will just decrement the reference count. Sometimes it may not be obvious whether a library was opened more than once, since it might have gotten loaded as a dependency of some other library without you knowing, unless it's a module local to your program, so it's probably not a good idea to rely on this "reset" behavior.
Employed Russian added:
Even if the library is
dlopen()ed anddlclose()d exactly once, and is not a dependency of something else, the act of referencing symbols from it (viadlsym()) will also increment the reference count (and make the library not unloadable); at least on Linux.
I have no idea if this information is accurate or not. In the future, please post new information as a comment or a new answer, not an edit to other people's answers. If you just edit someone else's answers, you make them take responsibility for the correctness of your answer, which they may not want.
是这样的吗? dlsym()也会增加dl的引用计数? 那什么时候引用计数会变为 0 呢?
本文探讨了当调用dlclose卸载动态库时,全局变量的行为变化。如果dlclose将引用计数减到零并实际卸载库,则重新加载该库会重置所有静态存储期变量。然而,若库被多次打开,除最后一次dlclose外,其余操作仅减少引用计数。
1020

被折叠的 条评论
为什么被折叠?



