I noticed a memory leak after updating to libusb 1.0.16 which added hotplug 
support, the leak is also present in the current git version. Basically I see 
libusb_device object leaking and tracked this down an unhandled hotplug left 
event that was created between my last call to libusb_handle_events_timeout() 
and libusb_exit() while I was shutting down the libusb_context.

In detail: the udev hotplug backend detects an USB remove event and calls 
usbi_disconnect_device(). This in turn removes the libusb_device from the 
ctx->usb_devs list and writes a left event to the ctx->hotplug_pipe. Finally 
handle_events() should read the left event and unref the libusb_device. But 
because this all happens after my last call to libusb_handle_events_timeout() 
and before libusb_exit() the left event isn't handled, the libusb_device 
doesn't get unrefed and leaks.

Any easy way to reproduce this is to add a second libusb_context to the 
hotplugtest.c example:

    diff --git a/examples/hotplugtest.c b/examples/hotplugtest.c
    index fef01af..3b4fe62 100644
    --- a/examples/hotplugtest.c
    +++ b/examples/hotplugtest.c
    @@ -60,12 +60,14 @@ int main(int argc, char *argv[])
            libusb_hotplug_callback_handle hp[2];
            int product_id, vendor_id, class_id;
            int rc;
    +       libusb_context *context2;
     
            vendor_id  = (argc > 1) ? strtol (argv[1], NULL, 0) : 0x045a;
            product_id = (argc > 2) ? strtol (argv[2], NULL, 0) : 0x5005;
            class_id   = (argc > 3) ? strtol (argv[3], NULL, 0) : 
LIBUSB_HOTPLUG_MATCH_ANY;
     
            libusb_init (NULL);
    +       libusb_init (&context2);
     
            if (!libusb_has_capability (LIBUSB_CAP_HAS_HOTPLUG)) {
                    printf ("Hotplug capabilites are not supported on this 
platform\n");
    @@ -93,5 +95,6 @@ int main(int argc, char *argv[])
                    libusb_handle_events (NULL);
            }
     
    +       libusb_exit (context2);
            libusb_exit (NULL);
     }

In this case libusb_handle_events_timeout() is never called for the second 
context, so hotplug left events are never handled and libusb_devices leak. One 
might argue that is example is artificial, but it demonstrates the effect of 
not calling libusb_handle_events() or similar between a hotplug left event and 
libusb_exit().

---
Reply to this email directly or view it on GitHub:
https://github.com/libusbx/libusbx/issues/150
------------------------------------------------------------------------------
October Webinars: Code for Performance
Free Intel webinars can help you accelerate application performance.
Explore tips for MPI, OpenMP, advanced profiling, and more. Get the most from 
the latest Intel processors and coprocessors. See abstracts and register >
http://pubads.g.doubleclick.net/gampad/clk?id=60133471&iu=/4140/ostg.clktrk
_______________________________________________
libusbx-devel mailing list
libusbx-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/libusbx-devel

Reply via email to