Skip to content

Commit

Permalink
improve issue that libusb_device instance leaked
Browse files Browse the repository at this point in the history
  • Loading branch information
saki4510t committed Nov 18, 2016
1 parent d7044d3 commit 2596242
Show file tree
Hide file tree
Showing 11 changed files with 21 additions and 667 deletions.
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 8 additions & 3 deletions libuvccamera/src/main/jni/libusb/libusb/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -1071,11 +1071,13 @@ int API_EXPORTED libusb_get_max_iso_packet_size(libusb_device *dev,
DEFAULT_VISIBILITY
libusb_device * LIBUSB_CALL libusb_ref_device(libusb_device *dev) {

int refcnt;
usbi_mutex_lock(&dev->lock);
{
dev->refcnt++;
refcnt = ++dev->refcnt;
}
usbi_mutex_unlock(&dev->lock);
// LOGI("refcnt=%d", refcnt);
return dev;
}

Expand All @@ -1096,6 +1098,7 @@ void API_EXPORTED libusb_unref_device(libusb_device *dev) {
refcnt = --dev->refcnt;
}
usbi_mutex_unlock(&dev->lock);
// LOGI("refcnt=%d", dev->refcnt);

if (refcnt == 0) {
usbi_dbg("destroy device %d.%d", dev->bus_number, dev->device_address);
Expand Down Expand Up @@ -1249,9 +1252,11 @@ libusb_device * LIBUSB_CALL libusb_get_device_with_fd(libusb_context *ctx,
ENTER();

struct libusb_device *device = NULL;
// android_generate_device内でusbi_alloc_deviceが呼ばれた時に参照カウンタは1
int ret = android_generate_device(ctx, &device, vid, pid, serial, fd, busnum, devaddr);
if (LIKELY(!ret)) {
libusb_ref_device(device); // これいるんかな? usbi_alloc_device内で既に呼ばれとるんやけど
if (ret) {
LOGD("android_generate_device failed:err=%d", ret);
device = NULL;
}

RET(device);
Expand Down
10 changes: 5 additions & 5 deletions libuvccamera/src/main/jni/libusb/libusb/os/android_usbfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -1300,7 +1300,6 @@ static int android_initialize_device(struct libusb_device *dev,
struct android_device_priv *priv = _device_priv(dev);
struct libusb_context *ctx = DEVICE_CTX(dev);
uint8_t desc[4096]; // max descriptor size is 4096 bytes
int descriptors_size = 512; /* Begin with a 1024 byte alloc */
int speed;
ssize_t r;

Expand All @@ -1310,15 +1309,15 @@ static int android_initialize_device(struct libusb_device *dev,
LOGD("cache descriptors in memory");

priv->descriptors_len = 0;
priv->fd = 0;
memset(desc, 0, sizeof(desc));
if (!lseek(fd, 0, SEEK_SET)) {
// ディスクリプタを読み込んでローカルキャッシュする
int length = read(fd, desc, sizeof(desc));
LOGD("Device::init read returned %d errno %d\n", length, errno);
if (length > 0) {
priv->fd = fd;
descriptors_size = length;
priv->descriptors = usbi_reallocf(priv->descriptors, descriptors_size);
priv->descriptors = usbi_reallocf(priv->descriptors, length);
if (UNLIKELY(!priv->descriptors)) {
RETURN(LIBUSB_ERROR_NO_MEM, int);
}
Expand Down Expand Up @@ -1389,7 +1388,7 @@ int android_generate_device(struct libusb_context *ctx, struct libusb_device **d
* a session ID. */
session_id = busnum << 8 | devaddr;
LOGD("allocating new device for %d/%d (session %ld)", busnum, devaddr, session_id);
*dev = usbi_alloc_device(ctx, session_id);
*dev = usbi_alloc_device(ctx, session_id); // この時点で参照カウンタ=1
if (UNLIKELY(!dev)) {
RETURN(LIBUSB_ERROR_NO_MEM, int);
}
Expand All @@ -1407,7 +1406,8 @@ int android_generate_device(struct libusb_context *ctx, struct libusb_device **d

out:
if (UNLIKELY(r < 0)) {
libusb_unref_device(*dev);
libusb_unref_device(*dev); // ここで参照カウンタが0になって破棄される
*dev = NULL;
} else {
usbi_connect_device(*dev);
}
Expand Down
2 changes: 1 addition & 1 deletion libuvccamera/src/main/jni/libuvc/android/jni/Android.mk
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ LOCAL_MODULE := libuvc_static
include $(BUILD_STATIC_LIBRARY)

######################################################################
# libuvc.so (shared library with static link to libjpeg-turbo)
# libuvc.so
######################################################################
include $(CLEAR_VARS)
LOCAL_MODULE_TAGS := optional
Expand Down
10 changes: 6 additions & 4 deletions libuvccamera/src/main/jni/libuvc/src/device.c
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ uvc_error_t uvc_get_device_with_fd(uvc_context_t *ctx, uvc_device_t **device,
LOGD("call libusb_get_device_with_fd");
struct libusb_device *usb_dev = libusb_get_device_with_fd(ctx->usb_ctx, vid, pid, serial, fd, busnum, devaddr);

if (usb_dev) {
if (LIKELY(usb_dev)) {
*device = malloc(sizeof(uvc_device_t/* *device */));
(*device)->ctx = ctx;
(*device)->ref = 0;
Expand All @@ -240,9 +240,9 @@ uvc_error_t uvc_get_device_with_fd(uvc_context_t *ctx, uvc_device_t **device,
} else {
LOGE("could not find specific device");
*device = NULL;
RETURN(UVC_ERROR_NO_DEVICE, int);
}

RETURN(UVC_ERROR_NO_DEVICE, int);
}

/** @brief Get the number of the bus to which the device is attached
Expand Down Expand Up @@ -791,7 +791,8 @@ const uvc_extension_unit_t *uvc_get_extension_units(uvc_device_handle_t *devh) {
void uvc_ref_device(uvc_device_t *dev) {
UVC_ENTER();

dev->ref++;
dev->ref++; // これ排他制御要るんちゃうかなぁ(。・_・。)
// LOGI("ref=%d", dev->ref);
libusb_ref_device(dev->usb_dev);

UVC_EXIT_VOID();
Expand All @@ -808,8 +809,9 @@ void uvc_unref_device(uvc_device_t *dev) {
UVC_ENTER();

libusb_unref_device(dev->usb_dev);
dev->ref--;
dev->ref--; // これ排他制御要るんちゃうかなぁ(。・_・。)

// LOGI("ref=%d", dev->ref);
if (dev->ref == 0) {
free(dev);
dev = NULL;
Expand Down
108 changes: 0 additions & 108 deletions usbCameraTest2/usbCameraTest2.iml

This file was deleted.

Loading

0 comments on commit 2596242

Please sign in to comment.