diff --git a/libgphoto2_port/libusb1/libusb1.c b/libgphoto2_port/libusb1/libusb1.c index c6b16aa..74b722f 100644 --- a/libgphoto2_port/libusb1/libusb1.c +++ b/libgphoto2_port/libusb1/libusb1.c @@ -84,6 +84,14 @@ static int gp_nrofdevs = 0; static struct libusb_device_descriptor *gp_descs; static libusb_device **gp_devs; +static void +free_devicelist(void) +{ + if (gp_devs != NULL) + libusb_free_device_list (gp_devs, 1); + gp_devs = NULL; +} + static ssize_t load_devicelist (libusb_context *ctx) { time_t xtime; @@ -91,7 +99,7 @@ load_devicelist (libusb_context *ctx) { time(&xtime); if (xtime != gp_devslastchecked) { if (gp_nrofdevs) - libusb_free_device_list (gp_devs, 1); + free_devicelist(); free (gp_descs); gp_nrofdevs = 0; gp_devs = NULL; @@ -267,12 +275,14 @@ static int gp_port_usb_init (GPPort *port) static int gp_port_usb_exit (GPPort *port) { + /* devices must be freed before exiting the USB context */ + free_devicelist(); + if (port->pl) { libusb_exit (port->pl->ctx); free (port->pl); port->pl = NULL; } - if (gp_devs) libusb_free_device_list (gp_devs, 1); free (gp_descs); return (GP_OK); }