当调用 dlclose 时,共享库中的全局变量会发生什么?
如果通过 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果 dlclose 将引用计数减少到零并且库实际上已卸载,则以后重新加载库时应将库中具有静态存储持续时间的所有变量重置为其原始值。
但是,如果库被打开多次,则除了最终调用 dlclose 之外的所有调用都只会减少引用计数。有时,一个库是否被多次打开可能并不明显,因为它可能在您不知道的情况下作为其他库的依赖项加载,除非它是您程序的本地模块,所以依赖它可能不是一个好主意关于这种“重置”行为。
俄罗斯雇员补充道:
我不知道这个信息是否准确。将来,请将新信息作为评论或新答案发布,而不是对其他人答案的编辑。如果您只是编辑其他人的答案,您就让他们对您答案的正确性负责,而他们可能不希望这样做。
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:
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.