Destroying the default context twice, even if it is refcounted, results in a segfault.

Fix submitted to libusbx as pull request #116.
Also fix this inside the Java wrapper, by adding our own refcount (similar to what was in original usb4java,
but allowing multiple calls with the default context), and making the access to said variable thread-safe.
Once libusbx fixes this, the above code can be removed.
This commit is contained in:
Luca Longinotti 2013-06-12 21:39:02 +02:00
parent 0ba3d8869f
commit a4d3ab69da
2 changed files with 20 additions and 12 deletions

View File

@ -23,6 +23,8 @@
#include "ConfigDescriptor.h"
#include "Transfer.h"
static int defaultContextRefcnt = 0;
/**
* Version getVersion()
*/
@ -44,7 +46,9 @@ JNIEXPORT jint JNICALL METHOD_NAME(LibUsb, init)
{
if (!context)
{
return libusb_init(NULL);
int result = libusb_init(NULL);
if (result == LIBUSB_SUCCESS) defaultContextRefcnt++;
return result;
}
else
{
@ -65,19 +69,23 @@ JNIEXPORT void JNICALL METHOD_NAME(LibUsb, exit)
JNIEnv *env, jclass class, jobject context
)
{
if (!context)
{
libusb_exit(NULL);
}
else
{
libusb_context *ctx = unwrapContext(env, context);
if (!ctx) return;
if (!context)
{
if (defaultContextRefcnt <= 0) return;
libusb_exit(NULL);
defaultContextRefcnt--;
}
else
{
libusb_context *ctx = unwrapContext(env, context);
if (!ctx) return;
libusb_exit(ctx);
resetContext(env, context);
}
}
}
/**

View File

@ -525,7 +525,7 @@ public final class LibUsb
*
* @see <a href="http://libusbx.sf.net/api-1.0/contexts.html">Contexts</a>
*/
public static native int init(final Context context);
public static synchronized native int init(final Context context);
/**
* Deinitialize libusb.
@ -537,7 +537,7 @@ public final class LibUsb
* The {@link Context} to deinitialize, or NULL for the default
* context.
*/
public static native void exit(final Context context);
public static synchronized native void exit(final Context context);
/**
* Set log message verbosity.