正如标题所说...我对 dlopen()
很感兴趣。我知道应用商店不允许这样做,但我对 iOS 上的这个感到好奇。
我遇到的问题是我可以创建一个 .dylib 文件,并且可以在运行时使用以下代码加载该文件
char *dylibPath = "/Applications/myapp.app/mydylib2.dylib";
void *libHandle = dlopen(dylibPath, RTLD_NOW);
if (libHandle != NULL) {
NSString * (*someMethod)() = dlsym(libHandle, "someMethod");
if (someMethod != NULL) {
NSLog(someMethod());
}
dlclose(libHandle);
}
这取自here .
我遇到的问题是,如果我更改 mydylib2.dylib,dlopen
不会加载重新编译的 .dylib 文件。相反,它解析函数 someMethod
的旧版本。
例如,如果 someMethod
首先返回 @"Hello"
并且我将其更改为 @"Hello World"
并重新编译,上面的代码将始终返回 @"Hello"
直到我在模拟器中重新启动应用程序。
有人知道为什么会这样吗?并提出解决方法,以便可以在运行时重新加载此 .dylib?
最佳答案
查看 man page对于 dlclose
,似乎有几个原因导致库无法卸载。
There are a couple of cases in which a dynamic library will never be unloaded: 1) the main executable links against it, 2) An API that does not supoort unloading (e.g. NSAddImage()) was used to load it or some other dynamic library that depends on it, 3) the dynamic library is in dyld's shared cache.
我怀疑您的问题是“dyld 共享缓存”。你可以尝试运行:
sudo update_dyld_shared_cache -force
在您替换库之后(不确定这是否在 iOS 上可用)。
您是否检查过dlclose
的返回值以查看它是成功还是失败?
关于ios - dlopen 有没有办法在 iOS 上重新加载 dylib 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12653167/