我有通用内核扩展,它在 C++ 示例中实现,用于开始和结束例程,而所有其他逻辑都存储在一个专用类中,继承自OSObject
.
它在模块启动例程时创建类,并在停止例程时释放它,如下面的代码所示:
class com_my_driver : public OSObject { ... };
...
..
.
com_my_driver *gDriver = NULL;
extern "C" kern_return_t my_driver_start(kmod_info_t * ki, void *d)
{
gDriver = new com_my_driver;
gDriver->init();
return KERN_SUCCESS;
}
extern "C" kern_return_t my_driver_stop(kmod_info_t *ki, void *d)
{
gDriver->release();
gDriver = nullptr;
return KERN_SUCCESS;
}
但是,当尝试卸载服务时,它无法到达停止例程,因为该类仍在被引用(我假设它到达了我释放此类的停止例程)。这是确切的日志消息:
(kernel) Can't unload kext com.my.driver; classes have instances:
(kernel) Kext com.my.driver class com_my_driver has 1 instance.
Failed to unload com.my.driver - (libkern/kext) kext is in use or retained (cannot unload).
在参考检查之前我可以在停止例行程序之前释放我的课程吗?
谢谢