dylib 析构函数没有被调用
我有一个 dylib,可以通过注入在 mac os x 上加载。 构造函数调用效果很好。
__attribute__((constructor))
static void initialize()
但是析构函数没有被调用?因此资源初始化泄漏。
__attribute__((destructor))
static void destroy()
- 如果应用程序退出,dylib 会自动卸载吗?
- 如果应用程序退出,注入的 dylib 是否会自动卸载?
- 我们如何在运行时从应用程序中卸载dylib?作为其注入代码,我可以访问私人区域。有命令可以做到这一点吗?
I have a dylib which I can load via injection on mac os x.
Constructor call works well.
__attribute__((constructor))
static void initialize()
But destructor does not get called? Thus resources initialized leaks.
__attribute__((destructor))
static void destroy()
- Does dylib gets unloaded automatically if application quits?
- Does injected dylib gets unloaded automatically if application quits?
- How can we unload dylib from the application at runtime? As its injection code I can access private area. Is there a command to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
1、2:不。当应用程序退出时,库并没有真正卸载——它们只是碰巧与进程的其余部分一起消失,就像其他资源(例如,文件句柄、映射内存、套接字、< em>etc)在退出时被释放。
3:取决于您如何注入库。例如,如果您使用
dlopen()
之类的方法加载它,您应该能够使用dlclose()
卸载该库;NSBundle
有类似的东西。请记住,卸载库是很混乱的。特别是,卸载包含任何 ObjC 类的库是不安全的,因为运行时可能缓存了对类的引用。
1, 2: No. Libraries aren't really unloaded when an application exits -- they just happen to disappear along with the rest of the process, in the same way that other resources (e.g, file handles, mapped memory, sockets, etc) are released on exit.
3: Depends on how you injected the library. If you loaded it using something like
dlopen()
, you should be able to unload the library usingdlclose()
, for instance;NSBundle
has something equivalent.Keep in mind that unloading libraries is messy. In particular, it's unsafe to unload a library which contains any ObjC classes, as the runtime may have cached references to your classes.