summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Plante <michael.plante@gmail.com>2010-01-24 17:15:36 -0600
committerPete Batard <pbatard@gmail.com>2010-01-24 23:34:25 +0000
commitb5eed0caccb63be98e51217443079556eb5b934d (patch)
treebdb1ddc78a95bff4eebfb10a5dc8d23f7fb13450
parent849d2915d6842b9d34de7947b87b64485273729a (diff)
downloadlibusb-b5eed0caccb63be98e51217443079556eb5b934d.tar.gz
r111: destroy core.c mutexes when done, and fix a few unlikely memory leaks
-rw-r--r--libusb/core.c14
1 files changed, 13 insertions, 1 deletions
diff --git a/libusb/core.c b/libusb/core.c
index 7455bbb..53c5988 100644
--- a/libusb/core.c
+++ b/libusb/core.c
@@ -883,8 +883,10 @@ API_EXPORTED int libusb_open(libusb_device *dev, libusb_device_handle **handle)
return LIBUSB_ERROR_NO_MEM;
r = pthread_mutex_init(&_handle->lock, NULL);
- if (r)
+ if (r) {
+ free(_handle);
return LIBUSB_ERROR_OTHER;
+ }
_handle->dev = libusb_ref_device(dev);
_handle->claimed_interfaces = 0;
@@ -893,6 +895,7 @@ API_EXPORTED int libusb_open(libusb_device *dev, libusb_device_handle **handle)
r = usbi_backend->open(_handle);
if (r < 0) {
libusb_unref_device(dev);
+ pthread_mutex_destroy(&_handle->lock);
free(_handle);
return (int)r;
}
@@ -1004,6 +1007,7 @@ static void do_close(struct libusb_context *ctx,
usbi_backend->close(dev_handle);
libusb_unref_device(dev_handle->dev);
+ pthread_mutex_destroy(&dev_handle->lock);
free(dev_handle);
}
@@ -1507,6 +1511,10 @@ API_EXPORTED int libusb_init(libusb_context **context)
if (!usbi_default_context) {
usbi_dbg("created default context");
usbi_default_context = ctx;
+ } else if (!context) {
+ pthread_mutex_unlock(&default_context_lock);
+ libusb_exit(ctx); /* free superfluous context; use default context */
+ return 0;
}
pthread_mutex_unlock(&default_context_lock);
@@ -1515,6 +1523,8 @@ API_EXPORTED int libusb_init(libusb_context **context)
return 0;
err:
+ pthread_mutex_destroy(&ctx->open_devs_lock);
+ pthread_mutex_destroy(&ctx->usb_devs_lock);
free(ctx);
return r;
}
@@ -1545,6 +1555,8 @@ API_EXPORTED void libusb_exit(struct libusb_context *ctx)
}
pthread_mutex_unlock(&default_context_lock);
+ pthread_mutex_destroy(&ctx->open_devs_lock);
+ pthread_mutex_destroy(&ctx->usb_devs_lock);
free(ctx);
}