Catch double-exit crash with IllegalStatException
This commit is contained in:
parent
fc4f7ced60
commit
2af4393d2e
@ -25,6 +25,8 @@
|
||||
|
||||
static JavaVM *jvm;
|
||||
|
||||
static int defaultContextInitialized = 0;
|
||||
|
||||
/**
|
||||
* Version getVersion()
|
||||
*/
|
||||
@ -46,7 +48,13 @@ JNIEXPORT jint JNICALL METHOD_NAME(LibUsb, init)
|
||||
{
|
||||
if (!context)
|
||||
{
|
||||
return libusb_init(NULL);
|
||||
if (defaultContextInitialized)
|
||||
{
|
||||
return illegalState(env, "Default context already initialized");
|
||||
}
|
||||
int result = libusb_init(NULL);
|
||||
if (!result) defaultContextInitialized = 1;
|
||||
return result;
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -67,8 +75,17 @@ JNIEXPORT void JNICALL METHOD_NAME(LibUsb, exit)
|
||||
{
|
||||
struct libusb_context *ctx = unwrapContext(env, context);
|
||||
if (!ctx && context) return;
|
||||
|
||||
if (!context && !defaultContextInitialized)
|
||||
{
|
||||
illegalState(env, "Default context not initialized");
|
||||
return;
|
||||
}
|
||||
libusb_exit(ctx);
|
||||
resetContext(env, context);
|
||||
if (context)
|
||||
resetContext(env, context);
|
||||
else
|
||||
defaultContextInitialized = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Binary file not shown.
@ -176,8 +176,16 @@ public class LibUSBTest
|
||||
assertEquals(LibUsb.SUCCESS, LibUsb.init(null));
|
||||
LibUsb.exit(null);
|
||||
|
||||
// Double-exit without a context should work
|
||||
LibUsb.exit(null);
|
||||
try
|
||||
{
|
||||
// Double-exit should throw exception
|
||||
LibUsb.exit(null);
|
||||
fail("Double-exit should throw IllegalStateException");
|
||||
}
|
||||
catch (IllegalStateException e)
|
||||
{
|
||||
// Expected behavior
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Loading…
Reference in New Issue
Block a user