src: http://stackoverflow.com/questions/8793099/unload-dynamic-library-needs-two-dlclose-calls

Question:

I have a dynamic library which I load using dlopen() and then unload using 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?

Answer:

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

In Cocoa applications, you should not use CFBundle routines to load and unload executable code, becauseCFBundle 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.

and this:

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

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).

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);}

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.

[转]unload dynamic library needs two dlclose() calls?的更多相关文章

  1. Dynamic Library Design Guidelines

    https://developer.apple.com/library/mac/documentation/DeveloperTools/Conceptual/DynamicLibraries/100 ...

  2. [工作积累] Android dynamic library & JNI_OnLoad

    Bionic libc doesn't load dependencies for current .so file (diff from Windows or Linux) so a explici ...

  3. PHP Startup: Unable to load dynamic library

    昨天帮一朋友配置服务器结果发现apache日志中有PHP Warning: PHP Startup: Unable to load dynamic library 提示了,然后调试数据库连接发现提示C ...

  4. PHP启动:动态库加载失败(PHP Warning: PHP Startup: Unable to load dynamic library '/usr/local/php7/lib/php/extensions/no-debug-non-zts-20151012/*.so')

    今天在linux上面智障一般搞了好久,本来想安装个swoole的,然后用  php -m 的命令想看下安装的PHP扩展库,发现有的扩展库有的可以出来,有的加载失败, 加载失败的错误类型: PHP Wa ...

  5. 安装完 swoole 后出现 PHP Warning: PHP Startup: Unable to load dynamic library 'swoole.so'的解决方法

    安装完 swoole 后出现 PHP Warning:  PHP Startup: Unable to load dynamic library 'swoole.so' (tried: /home/s ...

  6. ***XAMPP:报错 Unable to load dynamic library的解决方法

    A PHP Error was encountered Severity: Core Warning Message: PHP Startup: Unable to load dynamic libr ...

  7. A simple dynamic library implemented in C# 4.0 to deal with XML structure

    https://github.com/cardinals/XmlToObjectParser A simple dynamic library implemented in C# 4.0 to dea ...

  8. Unable to load dynamic library 'zip.so' on Centos 6.8 useing php7.3

    背景: Centos6.8服务器升级php版本,从7.1升级到7.3,常用扩展都安装完成之后,报:Class 'ZipArchive' not found.一看就是zip扩展没有,需要手动安装了. 中 ...

  9. tensorflow2.x 报错 Could not load dynamic library 'cudart64_101.dll'

    当我们使用 tensorflow 最新版本的时候 ,会出现这样的错误 -- ::] Could not load dynamic library 'cudart64_101.dll'; dlerror ...

随机推荐

  1. MVC基本概念和流程

    MVC基本概念和流程 MVC的概念 Model(模型):包含数据和行为.不过现在一般都分离开来:Value Object(数据) 和 服务层(行为). View(视图):负责进行模型的展示,一般就是展 ...

  2. 多线程相互排斥--mutex(二)

    不知道大家对多线程或多进程间的同步相互排斥的控制机制了解的怎么样,事实上有非常多种方法能够实现这个目的,可是这些方法事实上由4种最主要的方法实现.这4种最主要的方法详细定义例如以下:在这有讲得不正确的 ...

  3. Scrapy研究和探索(七)——如何防止被ban大集合策略

    说来设置的尝试download_delay少于1,不管对方是什么,以防止ban策略后.我终于成功ban该. 大约scrapy利用能看到以前的文章: http://blog.csdn.net/u0121 ...

  4. 【云图】如何制作全国KTV查询系统?

    原文:[云图]如何制作全国KTV查询系统? 摘要:本文以[唱吧]531麦霸音乐节为案例,详细解读了如何导入自有数据到高德云图,并进行检索和展示.最后,调起高德mobile地图来进行路线规划和周边查询. ...

  5. 【转】传递给Appium服务器以开启相应安卓Automation会话的Capabilities的几点说明

    原文地址:http://blog.csdn.net/zhubaitian/article/details/39431307 Desired Capabilities是由客户端发送给Appium服务器端 ...

  6. PHP和MySQL Web开发 原书第4版 高清文字版,有目录,附带源码

    PHP和MySQL Web开发  原书第4版:http://yunpan.cn/QCWIS25zmYTAn  提取码 fd9b PHP和MySQL Web开发  原书第4版源码:http://yunp ...

  7. OracleServiceORCL服务不见了怎么办

    用管理员身份运行命令提示符(CMD) 然后输入“oradim -new -sid orcl”即可

  8. EF 事物

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...

  9. 【转】height,posHeight和pixelHeight区别

    height 返回高度    带单位 posHeight    不带单位的数字 pixelHeight 转换成像素后的数字    不带单位 更多资料 http://www.1fan.com.cn

  10. 文档PDF开放

    108个大数据文档PDF开放下载 投递人 itwriter 发布于 2015-01-29 13:34 评论(13) 有2251人阅读   原文链接  [收藏]   « » 文/36 大数据 总有人问我 ...