darwin: Explicitly cleanup cached devices during the last libusb_exit()
authorChris Dickens <christopher.a.dickens@gmail.com>
Thu, 26 Mar 2020 22:50:04 +0000 (15:50 -0700)
committerChris Dickens <christopher.a.dickens@gmail.com>
Thu, 26 Mar 2020 22:50:04 +0000 (15:50 -0700)
commit1201bccf857aa2c126f6f568f5255b0aa6027ac2
treed50d2f89312b8341ae5fe5024adffda74f76b3ea
parent8f7f8560a7ff3db832748bef576bbfd5af527fcf
darwin: Explicitly cleanup cached devices during the last libusb_exit()

Deferring the cached devices cleanup until the "destructor" function is
called makes it appear as though libusb is leaking memory, especially if
heap allocations are analyzed after calling libusb_exit(). It can also
lead to devices staying on the list longer than they should, as seen by
the following sequence of events:

  libusb_init() <-- init_count is 0, async thread is started
    devices_scan_devices() <-- enumerates devices
  libusb_exit() <-- init_count is 0, async thread is stopped
  [one or more devices disconnected]

Because the async thread is stopped when device(s) are disconnected in
the above sequence, the disconnection event(s) will not be processed and
thus darwin_devices_detached() will not be called and the list will have
one or more stale entries until the "destructor" function is finally
called.

Address both of these shortcomings by cleaning up the cached devices
list after stopping the async thread.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
libusb/os/darwin_usb.c
libusb/version_nano.h