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. 惰性函数——JS高级

    我们先来看一下js的异步提交. XHR我们在原生的时候常常用到,因为常用到,我们更多把封装到了工具库中 先看下他最常用的实现 // 旧方法 function createXHR() { var xhr ...

  2. 兼容安卓的javaproject1.0

    <pre class="java" name="code"> //兼容安卓的系统package cn.com.likeshow; import ja ...

  3. SP服务商收益究竟有多大?

    揭秘spspsp服务商怎样盈利?代办sp服务商又称持增值电信----移动网信息服务许可证信息提供商,sp主要业务有短信彩信(手机报.短信群发.客服系统).WAP.彩铃.IVR.百宝箱.JAVA游戏.B ...

  4. 用DIV+css写Table

    做出的效果样式如下图, 1,首先考虑的是如何显示border,就像是分割代码,我把border分割为最外层DIV全border,和内层DIV的right和bottom的border,就是右边和下边. ...

  5. WebForm MapPageRoute 路由配置

    WebForm MapPageRoute 路由配置   MapPageRoute 应该是 ASP.NET 4.0 中的东西,但现在我是第一次使用它,使用场景是:MVC 混合使用 WebForm,然后对 ...

  6. OR导致笛卡尔积

    近期监控数据库,发现以下语句跑得很慢,原来运行计划走了导致笛卡尔积,来看以下语句: SQL> explain plan for 2 SELECT COUNT(*) 3 FROM "GD ...

  7. MVC生成CheckBoxList并对其验证

    原文:MVC生成CheckBoxList并对其验证 通过扩展方法,可以让CheckBox水平排列,生成CheckBoxList,正如"MVC扩展生成CheckBoxList并水平排列&quo ...

  8. CSS下背景属性background的使用方法

    背景颜色(background-color) CSS可以用纯色来作为背景,也可以将背景设置为透明,background相当于xhtml中的bgcolor. 它的两个值: transparent(默认值 ...

  9. Android中利用Handler实现消息的分发机制(三)

    在第二篇文章<Android中利用Handler实现消息的分发机制(一)>中,我们讲到主线程的Looper是Android系统在启动App的时候,已经帮我们创建好了,而假设在子线程中须要去 ...

  10. zoj 3665 数论 二分法 两个参数

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4888 标题两个参数,途径:小参数的枚举,然后二分法大参数 想想两个点.以后就不 ...