卸载动态库需要两个dlclose()调用? [英] unload dynamic library needs two dlclose() calls?

查看:1179
本文介绍了卸载动态库需要两个dlclose()调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我加载使用的dlopen(),然后卸载使用 dlclose()一个动态库;

I have a dynamic library which I load using dlopen() and then unload using dlclose();

如果我不包括任何目标C code 的dlopen()需要有一个 dlclose()调用哪个是预期的行为。但是,当我包括任何目标C code的目标,我有问题,我需要做两 dlclose()以卸载调用加载库。

If I dont include any objective c code dlopen() needs one dlclose() call which is expected behavior. But when I include any objective c code to target, I have problem that I need to do two dlclose() calls to the loaded library in order to unload.

这是不是正常现象?我怎样才能解决这个问题?

Is this something expected behavior? How can I fix it?

推荐答案

我知道你正在使用的dlopen ,而不是的CFBundle 一个NSBundle 。尽管如此, $c$c装载编程主题手册说的:

I realize that you are using dlopen, not CFBundle or NSBundle. Nevertheless, the Code Loading Programming Topics manual says this:

在Cocoa应用程序,则不应使用的CFBundle 程序加载和卸载可执行code,因为的CFBundle 本身不支持Objective-C的运行时间。 一个NSBundle 正确加载Objective-C的符号,到运行时系统,但也没有办法卸载可可捆包一旦加载由于运行时间限制。

In Cocoa applications, you should not use CFBundle routines to load and unload executable code, because CFBundle does not natively support the Objective-C runtime. NSBundle correctly loads Objective-C symbols into the runtime system, but there is no way to unload Cocoa bundles once loaded due to a runtime limitation.

和这样的:

由于在Objective-C的运行时系统中的限制,一个NSBundle 无法卸载可执行code。

Because of a limitation in the Objective-C runtime system, NSBundle cannot unload executable code.

这使我怀疑,当你加载你的图书馆,它本身注册的Objective-C运行时和运行时调用的dlopen 在图书馆试(或在某种程度上增加该库的引用计数)。

This makes me suspect that when you load your library, it registers itself with the Objective-C runtime, and the runtime calls dlopen on the library again (or somehow increases the library's reference count).

我搜索了Objective-C运行源$ C ​​$ C,发现

I searched the Objective-C runtime source code and found this:

// dylibs are not allowed to unload
// ...except those with image_info and nothing else (5359412)
if (result->mhdr->filetype == MH_DYLIB  &&  _hasObjcContents(result)) {
    dlopen(result->os.dl_info.dli_fname, RTLD_NOLOAD);
}

所以,是的,Objective-C的运行时呼吁的dlopen 您的图书馆专门被卸载prevent它。如果你欺骗并调用 dlclose 两次,你应该想到不好的事情发生。

So yes, the Objective-C runtime is calling dlopen on your library specifically to prevent it from being unloaded. If you cheat and call dlclose twice, you should expect bad things to happen.

这篇关于卸载动态库需要两个dlclose()调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆