I have written a device driver kext for a hot-plug SCSI device, based somewhat on Wagerlabs code (using a driver-user client-application model) and everything works. The only remaining concern is that the driver appears not to be consistently freed, especially if the application crashes. For example, when I try to unload the kext, even with the device disconnected and the application closed, there are still outstanding instances of the driver and user client (with the driver generally outnumbering the user client).
I have logging in the driver functions like free()
, and when I shut down the computer, I can see these being executed, so the instances can obviously still be terminated. What is the "right" way to ensure the driver instance is terminated and freed, even if the host application crashes, terminates improperly or things generally don't go to plan?
If you've got user client class instances when no user client app is running, then you're definitely retaining the user client instances more often than you're releasing them. For example, you might be keeping a retained reference to client instances in the main driver class. In your user client class's
stop()
method, make sure to remove that client instance from the driver.Another thing to watch out for: make sure you call superclass implementations from your overridden versions of the built-in IOService methods such as
stop()
,free()
etc. Not doing so will usually put the IO Kit into an inconsistent state.Finally, a useful technique for debugging retain leaks in I/O Kit drivers, is to actually log the retains and releases by overriding the methods with logging versions:
The macros in this code are defined in a header as follows:
I should explain that
taggedRetain()
andtaggedRelease()
are the actual underlying implementation ofretain()
andrelease()
- if you override the latter, you won't see any retains and releases coming from OSCollections, as they use the tagged versions (with a non-null tag).The backtrace generated by
OSReportWithBacktrace()
is unfortunately just a bunch of hex pointers, but you can look those up using gdb.In any case, by logging retains and releases for your objects, you can go through all retains and make sure they are matched by a release in the right place. Watch out for cycles!