usbLinkOpen() in the develop branch calls libusb_ref_device() to increment the ref, but I can not find any code that will decrement it using libusb_unref_device().
This is suspicious and may have unwanted sideaffects. libusb doc writes that ref/unref need to be matched and when ref=0 a device is finally destroyed.
libusb_ref_device() only exists one place...it is within refLibusbDeviceByName()
|
libusb_ref_device(devs[i]); |
That function above refLibusbDeviceByName() is called by code in three locations...
usb_boot() location
Looks correct to me. It has a matching unref.
|
if(refLibusbDeviceByName(addr, &dev) == X_LINK_PLATFORM_SUCCESS){ |
|
break; |
|
} |
|
std::this_thread::sleep_for(milliseconds(10)); |
|
} while(steady_clock::now() - t1 < DEFAULT_CONNECT_TIMEOUT); |
|
|
|
if(dev == nullptr) { |
|
return X_LINK_PLATFORM_DEVICE_NOT_FOUND; |
|
} |
|
|
|
auto t2 = steady_clock::now(); |
|
do { |
|
if((res = usb_open_device(dev, &endpoint, h)) == LIBUSB_SUCCESS){ |
|
break; |
|
} |
|
std::this_thread::sleep_for(milliseconds(100)); |
|
} while(steady_clock::now() - t2 < DEFAULT_CONNECT_TIMEOUT); |
|
|
|
if(res == LIBUSB_SUCCESS) { |
|
rc = send_file(h, endpoint, mvcmd, size, bcdusb); |
|
libusb_release_interface(h, 0); |
|
libusb_close(h); |
|
} else { |
|
if(res == LIBUSB_ERROR_ACCESS) { |
|
rc = X_LINK_PLATFORM_INSUFFICIENT_PERMISSIONS; |
|
} else if(res == LIBUSB_ERROR_BUSY) { |
|
rc = X_LINK_PLATFORM_DEVICE_BUSY; |
|
} else { |
|
rc = X_LINK_PLATFORM_ERROR; |
|
} |
|
} |
|
|
|
if (dev) { |
|
libusb_unref_device(dev); |
usbLinkBootBootloader() location
Looks correct to me. It has a matching unref.
|
auto refErr = refLibusbDeviceByName(path, &dev); |
usbLinkOpen location
Looks suspicious. It calls refLibusbDeviceByName which increments the ref. But nothing calls unref. If I guess, I would think there should be an unref in usbLinkClose but there is not.
|
if(refLibusbDeviceByName(path, &dev) == X_LINK_PLATFORM_SUCCESS){ |
usbLinkOpen()in thedevelopbranch callslibusb_ref_device()to increment the ref, but I can not find any code that will decrement it usinglibusb_unref_device().This is suspicious and may have unwanted sideaffects. libusb doc writes that ref/unref need to be matched and when ref=0 a device is finally destroyed.
libusb_ref_device()only exists one place...it is withinrefLibusbDeviceByName()XLink/src/pc/protocols/usb_host.cpp
Line 244 in 457b23f
That function above
refLibusbDeviceByName()is called by code in three locations...usb_boot()locationLooks correct to me. It has a matching
unref.XLink/src/pc/protocols/usb_host.cpp
Lines 632 to 665 in 457b23f
usbLinkBootBootloader()locationLooks correct to me. It has a matching
unref.XLink/src/pc/protocols/usb_host.cpp
Line 714 in 457b23f
usbLinkOpenlocationLooks suspicious. It calls
refLibusbDeviceByNamewhich increments the ref. But nothing callsunref. If I guess, I would think there should be an unref inusbLinkClosebut there is not.XLink/src/pc/protocols/usb_host.cpp
Line 687 in 457b23f