当调用 dlclose 时,共享库中的全局变量会发生什么?

发布于 2024-10-13 08:14:48 字数 132 浏览 2 评论 0原文

如果通过 dlopen 和 dlclose 机制使用共享库(或 DLL),并且创建的共享库有一些内存来自堆的全局变量,那么当调用 dlclose 时这些变量和内存会发生什么?

如果在同一个进程中,再次调用 dlopen,会有什么行为?

If a shared library (or a DLL) is being used through dlopen and dlclose mechanism and if the shared library created has some global variables whose memory comes from the heap, then what will happen to those variables and the memory when dlclose is called?

If in the same process, dlopen is called again, what will be the behaviour?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

耳钉梦 2024-10-20 08:14:48

如果 dlclose 将引用计数减少到零并且库实际上已卸载,则以后重新加载库时应将库中具有静态存储持续时间的所有变量重置为其原始值。

但是,如果库被打开多次,则除了最终调用 dlclose 之外的所有调用都只会减少引用计数。有时,一个库是否被多次打开可能并不明显,因为它可能在您不知道的情况下作为其他库的依赖项加载,除非它是您程序的本地模块,所以依赖它可能不是一个好主意关于这种“重置”行为。

俄罗斯雇员补充道:

即使库被 dlopen()ed 和 dlclose()d 恰好一次,并且不是其他东西的依赖项,从中引用符号的行为(通过dlsym())也会增加引用计数(并使库不可卸载);至少在 Linux 上是这样。

我不知道这个信息是否准确。将来,请将新信息作为评论或新答案发布,而不是对其他人答案的编辑。如果您只是编辑其他人的答案,您就让他们对您答案的正确性负责,而他们可能不希望这样做。

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 and dlclose()d exactly once, and is not a dependency of something else, the act of referencing symbols from it (via dlsym()) 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文