diff --git a/src/main/c/src/LibUsb.c b/src/main/c/src/LibUsb.c index d02c89d..92a407e 100644 --- a/src/main/c/src/LibUsb.c +++ b/src/main/c/src/LibUsb.c @@ -66,7 +66,7 @@ JNIEXPORT void JNICALL METHOD_NAME(LibUsb, exit) ) { struct libusb_context *ctx = unwrapContext(env, context); - if (!ctx) return; + if (!ctx && context) return; libusb_exit(ctx); resetContext(env, context); } @@ -80,7 +80,7 @@ JNIEXPORT void JNICALL METHOD_NAME(LibUsb, setDebug) ) { struct libusb_context *ctx = unwrapContext(env, context); - if (!ctx) return; + if (!ctx && context) return; libusb_set_debug(ctx, level); } @@ -94,7 +94,7 @@ JNIEXPORT jint JNICALL METHOD_NAME(LibUsb, getDeviceList) { NOT_NULL(env, deviceList, return 0); struct libusb_context *ctx = unwrapContext(env, context); - if (!ctx) return 0; + if (!ctx && context) return 0; libusb_device **list; ssize_t result = libusb_get_device_list(ctx, &list); if (result >= 0) setDeviceList(env, list, result, deviceList); @@ -159,7 +159,7 @@ JNIEXPORT jint JNICALL METHOD_NAME(LibUsb, getPortPath) NOT_NULL(env, device, return 0); NOT_NULL(env, path, return 0); struct libusb_context *ctx = unwrapContext(env, context); - if (!ctx) return 0; + if (!ctx && context) return 0; libusb_device *dev = unwrapDevice(env, device); if (!dev) return 0; jsize size = (*env)->GetArrayLength(env, path); @@ -304,7 +304,7 @@ JNIEXPORT jobject JNICALL METHOD_NAME(LibUsb, openDeviceWithVidPid) ) { struct libusb_context *ctx = unwrapContext(env, context); - if (!ctx) return NULL; + if (!ctx && context) return NULL; return wrapDeviceHandle(env, libusb_open_device_with_vid_pid( ctx, vendorId, productId)); } @@ -775,7 +775,7 @@ JNIEXPORT jint JNICALL METHOD_NAME(LibUsb, tryLockEvents) ) { struct libusb_context *ctx = unwrapContext(env, context); - if (!ctx) return 0; + if (!ctx && context) return 0; return libusb_try_lock_events(ctx); } @@ -788,7 +788,7 @@ JNIEXPORT void JNICALL METHOD_NAME(LibUsb, lockEvents) ) { struct libusb_context *ctx = unwrapContext(env, context); - if (!ctx) return; + if (!ctx && context) return; libusb_lock_events(ctx); } @@ -801,7 +801,7 @@ JNIEXPORT void JNICALL METHOD_NAME(LibUsb, unlockEvents) ) { struct libusb_context *ctx = unwrapContext(env, context); - if (!ctx) return; + if (!ctx && context) return; libusb_unlock_events(ctx); } @@ -814,7 +814,7 @@ JNIEXPORT jint JNICALL METHOD_NAME(LibUsb, eventHandlingOk) ) { struct libusb_context *ctx = unwrapContext(env, context); - if (!ctx) return 0; + if (!ctx && context) return 0; return libusb_event_handling_ok(ctx); } @@ -827,7 +827,7 @@ JNIEXPORT jint JNICALL METHOD_NAME(LibUsb, eventHandlerActive) ) { struct libusb_context *ctx = unwrapContext(env, context); - if (!ctx) return 0; + if (!ctx && context) return 0; return libusb_event_handler_active(ctx); } @@ -840,7 +840,7 @@ JNIEXPORT void JNICALL METHOD_NAME(LibUsb, lockEventWaiters) ) { struct libusb_context *ctx = unwrapContext(env, context); - if (!ctx) return; + if (!ctx && context) return; libusb_lock_event_waiters(ctx); } @@ -853,7 +853,7 @@ JNIEXPORT void JNICALL METHOD_NAME(LibUsb, unlockEventWaiters) ) { struct libusb_context *ctx = unwrapContext(env, context); - if (!ctx) return; + if (!ctx && context) return; libusb_unlock_event_waiters(ctx); } @@ -866,7 +866,7 @@ JNIEXPORT jint JNICALL METHOD_NAME(LibUsb, waitForEvent) ) { struct libusb_context *ctx = unwrapContext(env, context); - if (!ctx) return 0; + if (!ctx && context) return 0; struct timeval tv; tv.tv_sec = timeout / 1000000; tv.tv_usec = timeout % 1000000; @@ -883,7 +883,7 @@ JNIEXPORT jint JNICALL METHOD_NAME(LibUsb, handleEventsTimeoutCompleted) ) { struct libusb_context *ctx = unwrapContext(env, context); - if (!ctx) return 0; + if (!ctx && context) return 0; struct timeval tv; tv.tv_sec = timeout / 1000000; tv.tv_usec = timeout % 1000000; @@ -907,7 +907,7 @@ JNIEXPORT jint JNICALL METHOD_NAME(LibUsb, handleEventsTimeout) ) { struct libusb_context *ctx = unwrapContext(env, context); - if (!ctx) return 0; + if (!ctx && context) return 0; struct timeval tv; tv.tv_sec = timeout / 1000000; tv.tv_usec = timeout % 1000000; @@ -923,7 +923,7 @@ JNIEXPORT jint JNICALL METHOD_NAME(LibUsb, handleEvents) ) { struct libusb_context *ctx = unwrapContext(env, context); - if (!ctx) return 0; + if (!ctx && context) return 0; return libusb_handle_events(ctx); } @@ -936,7 +936,7 @@ JNIEXPORT jint JNICALL METHOD_NAME(LibUsb, handleEventsCompleted) ) { struct libusb_context *ctx = unwrapContext(env, context); - if (!ctx) return 0; + if (!ctx && context) return 0; int complete; int result = libusb_handle_events_completed(ctx, &complete); if (!result && completed) @@ -957,7 +957,7 @@ JNIEXPORT jint JNICALL METHOD_NAME(LibUsb, handleEventsLocked) ) { struct libusb_context *ctx = unwrapContext(env, context); - if (!ctx) return 0; + if (!ctx && context) return 0; struct timeval tv; tv.tv_sec = timeout / 1000000; tv.tv_usec = timeout % 1000000; @@ -973,7 +973,7 @@ JNIEXPORT jint JNICALL METHOD_NAME(LibUsb, pollfdsHandleTimeouts) ) { struct libusb_context *ctx = unwrapContext(env, context); - if (!ctx) return 0; + if (!ctx && context) return 0; return libusb_pollfds_handle_timeouts(ctx); } @@ -986,7 +986,7 @@ JNIEXPORT jint JNICALL METHOD_NAME(LibUsb, getNextTimeout) ) { struct libusb_context *ctx = unwrapContext(env, context); - if (!ctx) return 0; + if (!ctx && context) return 0; struct timeval tv; int result = libusb_get_next_timeout(ctx, &tv); if (result == 1) @@ -1041,7 +1041,7 @@ JNIEXPORT void JNICALL METHOD_NAME(LibUsb, setPollfdNotifiers) ) { struct libusb_context *ctx = unwrapContext(env, context); - if (!ctx) return; + if (!ctx && context) return; (*env)->GetJavaVM(env, &jvm); libusb_set_pollfd_notifiers(ctx, triggerPollfdAdded, triggerPollfdRemoved, NULL); @@ -1056,7 +1056,7 @@ JNIEXPORT void JNICALL METHOD_NAME(LibUsb, unsetPollfdNotifiers) ) { struct libusb_context *ctx = unwrapContext(env, context); - if (!ctx) return; + if (!ctx && context) return; libusb_set_pollfd_notifiers(ctx, NULL, NULL, NULL); } diff --git a/src/main/resources/de/ailis/usb4java/libusb/linux-x86_64/libusb4java.so b/src/main/resources/de/ailis/usb4java/libusb/linux-x86_64/libusb4java.so index 589f3bb..8f60525 100644 Binary files a/src/main/resources/de/ailis/usb4java/libusb/linux-x86_64/libusb4java.so and b/src/main/resources/de/ailis/usb4java/libusb/linux-x86_64/libusb4java.so differ diff --git a/src/test/java/de/ailis/usb4java/libusb/LibUSBDeviceTest.java b/src/test/java/de/ailis/usb4java/libusb/LibUSBDeviceTest.java index 8763142..018eded 100644 --- a/src/test/java/de/ailis/usb4java/libusb/LibUSBDeviceTest.java +++ b/src/test/java/de/ailis/usb4java/libusb/LibUSBDeviceTest.java @@ -259,16 +259,7 @@ public class LibUSBDeviceTest public void testGetParentWithoutDevice() { assumeUsbTestsEnabled(); - DeviceList list = new DeviceList(); - LibUsb.getDeviceList(this.context, list); - try - { - LibUsb.getParent(null); - } - finally - { - LibUsb.freeDeviceList(list, true); - } + LibUsb.getParent(null); } /** @@ -1156,4 +1147,28 @@ public class LibUSBDeviceTest // Expected behavior } } + + /** + * Tests the {@link LibUsb#getDeviceList(Context, DeviceList)} and + * LibUsb#freeDeviceList(DeviceList, boolean)} methods with the default + * context. + */ + @Test + public void testGetAndFreeDeviceListWithDefaultContext() + { + assumeUsbTestsEnabled(); + DeviceList list = new DeviceList(); + assertTrue(LibUsb.getDeviceList(null, list) >= 0); + LibUsb.freeDeviceList(list, true); + + try + { + LibUsb.freeDeviceList(list, true); + fail("Double-free should throw IllegalStateException"); + } + catch (IllegalStateException e) + { + // Expected behavior + } + } }