Fix memory leakage pathe with no V2 device found
authorUwe Bonnes <bon@elektron.ikp.physik.tu-darmstadt.de>
Sat, 10 Dec 2011 16:59:20 +0000 (17:59 +0100)
committerUwe Bonnes <bon@elektron.ikp.physik.tu-darmstadt.de>
Sat, 10 Dec 2011 16:59:20 +0000 (17:59 +0100)
src/stlink-usb.c

index 58c4e1d334f3733b8565afe0f4ed85b25334309c..1320831d1ff3b0e1143a5641f375f037eef8f876 100644 (file)
@@ -576,9 +576,8 @@ stlink_t* stlink_open_usb(const int verbose) {
     slu->usb_handle = libusb_open_device_with_vid_pid(slu->libusb_ctx, USB_ST_VID, USB_STLINK_32L_PID);
     if (slu->usb_handle == NULL) {
                // TODO - free usb context too...
-        free(slu);
                WLOG("Couldn't find any ST-Link/V2 devices");
-        return NULL;
+        goto on_error;
     }
     
     if (libusb_kernel_driver_active(slu->usb_handle, 0) == 1) {
@@ -660,6 +659,8 @@ on_libusb_error:
     return sl;
 
 on_error:
+    if( slu->libusb_ctx)
+       libusb_exit(slu->libusb_ctx);
     if (sl != NULL) free(sl);
     if (slu != NULL) free(slu);
     return 0;