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. ASP.NET MVC上传文件----uploadify的使用

    课程设计需要实现上传文件模块,本来ASP.NET是有内置的控件,但是ASP.NET MVC没有,所以就有两种方法:自定义和采用第三方插件.由于时间的关系,故采用第三方插件:uploadify. upl ...

  2. linux_inux中find批量删除空文件及空文件夹脚本

    1.{}和之间有一个空格 2.find . -name 之间也有空格 3.exec 是一个后续的命令,{}内的内容代表前面查找出来的文件 linux下批量删除空文件(大小等于0的文件)的方法 rm - ...

  3. Redhat Linux下的python版本号升级

    运行#Python与#python -V,看到版本是2.4.3,非常老了,并且之前写的都是跑在python3.X上面的,3.X和2.X有非常多不同, 有兴趣的朋友能够參考下这篇文章:  http:// ...

  4. Android开发手册 (Android的手工教程MtAndroid开发手册)

    放出版许可协议 1.0 或者更新版本号. 未经版权全部者明白授权,禁止发行本文档及其被实质上改动的版本号.  未经版权全部者事先授权.禁止将此作品及其衍生作品以标准(纸质)书籍形式发行. 假设有兴趣再 ...

  5. 【百度地图API】建立全国银行位置查询系统(一)——如何创建地图

    原文:[百度地图API]建立全国银行位置查询系统(一)--如何创建地图 <摘要>你将在第一章中学会以下知识: 如何创建一个网页文件 怎样利用百度地图API建立一张2D地图,以及3D地图 如 ...

  6. app后端设计(0)--总文件夹

    原文:http://blog.csdn.net/newjueqi/article/details/19003775 做了接近两年app相关的系统架构,api设计,先后在两个创业公司中工作,经历过手机网 ...

  7. .net的自定义JS控件,运用了 面向对象的思想 封装 了 控件(.net自定义控件开发的第一天)

    大家好!我叫刘晶,很高兴你能看到我分享的文章!希望能对你有帮助! 首先我们来看下几个例子 ,就能看到 如何 自定义控件! 业务需求: 制作  一个   属于 自己的    按钮 对象    ,然后 像 ...

  8. Redhat Enterprise server 6.3 构造VPN

    一.软体 dkms.kernel_ppp_mppe.pptpd 二.下载软件 wget http://sourceforge.net/projects/poptop/files/mppe%20modu ...

  9. JS全选功能代码优化

    原文:JS全选功能代码优化 JS全选功能代码优化 最近在看javascript MVC那本书,也感觉到自己写的代码也并不优雅,所以一直在想 用另一种模式来编写JS代码,所以针对之前的简单的JS全选功能 ...

  10. sql优化的50中方法

    查询速度慢的原因很多,常见如下几种:    1.没有索引或者没有用到索引(这是查询慢最常见的问题,是程序设计的缺陷)    2.I/O吞吐量小,形成了瓶颈效应.    3.没有创建计算列导致查询不优化 ...