I've got Generic Kernel Extension which is implemented in C++ example for the start and end routines, whereas all the other logic is stored within a dedicated class inherit from OSObject
.
it creates the class upon module start routine, and release it upon stop routine as can be shown in the code below :
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;
}
However, when trying to unload the service, It cannot reach the stop routine since the class is still being referenced (I assumed it reach to the stop routine where I release this class). Here's the exact log message:
(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).
Is there any other even where I can release my class prior to the stop routine before the reference inspection ?
thanks