Christopher Flordeliza
2017-07-03 00:31:00 UTC
Hi All,
libusb_get_string_descriptor_ascii throw an illegal access exception when called after I unplugged and plugged my USB device. I revised the listdev project in the libusb workspace to:
const libusb_device_handle* Open(const libusb_context* context)
{
struct libusb_device_descriptor deviceDescriptor;
libusb_device_handle* handle = NULL;
/* list devices */
libusb_device **devices = 0;
ssize_t deviceCount = libusb_get_device_list(context, &devices);
for (int index = 0; index < deviceCount; index++)
{
libusb_device *currentDevice = devices[index];
if ((libusb_get_device_descriptor(currentDevice, &deviceDescriptor) == 0)
&& (deviceDescriptor.idVendor == 0x0525)
&& (deviceDescriptor.idProduct == 0xA4A4))
{
if (0 == libusb_open(currentDevice, &handle))
{
unsigned char sn_ascii[14];
sn_ascii[13] = 0;
int snlen = 0;
// Match serial number if a serial number was specified
if (0 < (snlen = libusb_get_string_descriptor_ascii(handle, deviceDescriptor.iSerialNumber,
sn_ascii, sizeof(sn_ascii))))
{
printf(" SERIAL: %s ---", sn_ascii);
}
}
}
}
libusb_free_device_list(devices, 1);
return handle;
}
static boolean Close(const libusb_context* context, const libusb_device_handle* handle)
{
boolean result = TRUE;
libusb_close(handle);
return result;
}
int main(void)
{
int result = 0;
int testMax = 100000;
int testCounter = 1;
int r;
ssize_t cnt;
libusb_context* context = NULL;
const libusb_device_handle* handle = NULL;
if (libusb_init(&context) < 0)
{
return -1;
}
do
{
handle = Open(context);
printf("Test #%08d: %s!\n", testCounter, handle ? "SUCCESS" : "FAILED");
if (handle)
{
Close(context, handle);
}
testCounter++;
} while (testCounter <= testMax);
libusb_exit(context);
return result;
}
The crash happens frequently in Windows 7 (64bit) and seldom in Windows 10 (64bit). Please note, the code was compiled in windows 10 and was run in Windows 7. To reproduce the problem:
1. Compile the above code in Windows 10 (Win32)
2. Run the exe in Windows 7
3. While running, unplug the USB device
4. Plug again the USB device, crash happens.
I changed libusb_free_device_list(devices, 1); to libusb_free_device_list(devices, 0); to fix the crash. After running it again many times the crash only happened 1 time, I am not sure if the cause of the crash is already fixed by changing libusb_free_device_list(devices, 0).
Please help me in pointing out what I am doing wrong here.
Regards
Christopher
libusb_get_string_descriptor_ascii throw an illegal access exception when called after I unplugged and plugged my USB device. I revised the listdev project in the libusb workspace to:
const libusb_device_handle* Open(const libusb_context* context)
{
struct libusb_device_descriptor deviceDescriptor;
libusb_device_handle* handle = NULL;
/* list devices */
libusb_device **devices = 0;
ssize_t deviceCount = libusb_get_device_list(context, &devices);
for (int index = 0; index < deviceCount; index++)
{
libusb_device *currentDevice = devices[index];
if ((libusb_get_device_descriptor(currentDevice, &deviceDescriptor) == 0)
&& (deviceDescriptor.idVendor == 0x0525)
&& (deviceDescriptor.idProduct == 0xA4A4))
{
if (0 == libusb_open(currentDevice, &handle))
{
unsigned char sn_ascii[14];
sn_ascii[13] = 0;
int snlen = 0;
// Match serial number if a serial number was specified
if (0 < (snlen = libusb_get_string_descriptor_ascii(handle, deviceDescriptor.iSerialNumber,
sn_ascii, sizeof(sn_ascii))))
{
printf(" SERIAL: %s ---", sn_ascii);
}
}
}
}
libusb_free_device_list(devices, 1);
return handle;
}
static boolean Close(const libusb_context* context, const libusb_device_handle* handle)
{
boolean result = TRUE;
libusb_close(handle);
return result;
}
int main(void)
{
int result = 0;
int testMax = 100000;
int testCounter = 1;
int r;
ssize_t cnt;
libusb_context* context = NULL;
const libusb_device_handle* handle = NULL;
if (libusb_init(&context) < 0)
{
return -1;
}
do
{
handle = Open(context);
printf("Test #%08d: %s!\n", testCounter, handle ? "SUCCESS" : "FAILED");
if (handle)
{
Close(context, handle);
}
testCounter++;
} while (testCounter <= testMax);
libusb_exit(context);
return result;
}
The crash happens frequently in Windows 7 (64bit) and seldom in Windows 10 (64bit). Please note, the code was compiled in windows 10 and was run in Windows 7. To reproduce the problem:
1. Compile the above code in Windows 10 (Win32)
2. Run the exe in Windows 7
3. While running, unplug the USB device
4. Plug again the USB device, crash happens.
I changed libusb_free_device_list(devices, 1); to libusb_free_device_list(devices, 0); to fix the crash. After running it again many times the crash only happened 1 time, I am not sure if the cause of the crash is already fixed by changing libusb_free_device_list(devices, 0).
Please help me in pointing out what I am doing wrong here.
Regards
Christopher