title: OSX Kext not being properly freed
body: <p>I have written a device driver kext for a hot-plug SCSI device, based somewhat on <a href="http://wagerlabs.com/blog/2008/02/04/writing-a-mac-osx-usb-device-driver-that-implements-scsi-pass-through/" rel="nofollow" title="Wagerlabs code">Wagerlabs code</a> (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).</p>
  
  <p>I have logging in the driver functions like <code>free()</code>,  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?</p>