Skip to content

Commit 8814257

Browse files
thomasroeferqdot
authored andcommitted
Fixed a memory leak in the MacOS version of libusb
Signed-off-by: Kyle Machulis <[email protected]>
1 parent 57b8d64 commit 8814257

File tree

1 file changed

+40
-8
lines changed

1 file changed

+40
-8
lines changed

platform/osx/libusb-osx-kinect.diff

+40-8
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
*
2626
* 1. struct usbi_transfer
2727
diff --git a/libusb/os/darwin_usb.c b/libusb/os/darwin_usb.c
28-
index 646c938..3900241 100644
28+
index 646c938..79ba6c6 100644
2929
--- a/libusb/os/darwin_usb.c
3030
+++ b/libusb/os/darwin_usb.c
3131
@@ -1137,6 +1137,8 @@ static int submit_bulk_transfer(struct usbi_transfer *itransfer) {
@@ -37,7 +37,31 @@ index 646c938..3900241 100644
3737
if (is_read)
3838
ret = (*(cInterface->interface))->ReadPipeAsyncTO(cInterface->interface, pipeRef, transfer->buffer,
3939
transfer->length, transfer->timeout, transfer->timeout,
40-
@@ -1198,9 +1200,11 @@ static int submit_iso_transfer(struct usbi_transfer *itransfer) {
40+
@@ -1171,10 +1173,19 @@ static int submit_iso_transfer(struct usbi_transfer *itransfer) {
41+
/* are we reading or writing? */
42+
is_read = transfer->endpoint & LIBUSB_ENDPOINT_IN;
43+
44+
- /* construct an array of IOUSBIsocFrames */
45+
- tpriv->isoc_framelist = (IOUSBIsocFrame*) calloc (transfer->num_iso_packets, sizeof(IOUSBIsocFrame));
46+
- if (!tpriv->isoc_framelist)
47+
- return LIBUSB_ERROR_NO_MEM;
48+
+ /* construct an array of IOUSBIsocFrames, reuse the old one if possible */
49+
+ if (tpriv->isoc_framelist != NULL && tpriv->num_iso_packets != transfer->num_iso_packets)
50+
+ {
51+
+ free(tpriv->isoc_framelist);
52+
+ tpriv->isoc_framelist = NULL;
53+
+ }
54+
+ if (tpriv->isoc_framelist == NULL)
55+
+ {
56+
+ tpriv->isoc_framelist = (IOUSBIsocFrame*) calloc (transfer->num_iso_packets, sizeof(IOUSBIsocFrame));
57+
+ tpriv->num_iso_packets = transfer->num_iso_packets;
58+
+ if (!tpriv->isoc_framelist)
59+
+ return LIBUSB_ERROR_NO_MEM;
60+
+ }
61+
62+
/* copy the frame list from the libusb descriptor (the structures differ only is member order) */
63+
for (i = 0 ; i < transfer->num_iso_packets ; i++)
64+
@@ -1198,9 +1209,11 @@ static int submit_iso_transfer(struct usbi_transfer *itransfer) {
4165

4266
return darwin_to_libusb (kresult);
4367
}
@@ -51,15 +75,15 @@ index 646c938..3900241 100644
5175

5276
/* submit the request */
5377
if (is_read)
54-
@@ -1211,6 +1215,7 @@ static int submit_iso_transfer(struct usbi_transfer *itransfer) {
78+
@@ -1211,6 +1224,7 @@ static int submit_iso_transfer(struct usbi_transfer *itransfer) {
5579
kresult = (*(cInterface->interface))->WriteIsochPipeAsync(cInterface->interface, pipeRef, transfer->buffer, frame,
5680
transfer->num_iso_packets, tpriv->isoc_framelist, darwin_async_io_callback,
5781
itransfer);
5882
+ cInterface->frames[transfer->endpoint] = frame + transfer->num_iso_packets / 8;
5983

6084
if (kresult != kIOReturnSuccess) {
6185
usbi_err (TRANSFER_CTX (transfer), "isochronous transfer failed (dir: %s): %s", is_read ? "In" : "Out",
62-
@@ -1243,6 +1248,8 @@ static int submit_control_transfer(struct usbi_transfer *itransfer) {
86+
@@ -1243,6 +1257,8 @@ static int submit_control_transfer(struct usbi_transfer *itransfer) {
6387
tpriv->req.pData = transfer->buffer + LIBUSB_CONTROL_SETUP_SIZE;
6488
tpriv->req.completionTimeout = transfer->timeout;
6589
tpriv->req.noDataTimeout = transfer->timeout;
@@ -68,7 +92,7 @@ index 646c938..3900241 100644
6892

6993
/* all transfers in libusb-1.0 are async */
7094
kresult = (*(dpriv->device))->DeviceRequestAsyncTO(dpriv->device, &(tpriv->req), darwin_async_io_callback, itransfer);
71-
@@ -1358,6 +1365,9 @@ static void darwin_async_io_callback (void *refcon, IOReturn result, void *arg0)
95+
@@ -1358,6 +1374,9 @@ static void darwin_async_io_callback (void *refcon, IOReturn result, void *arg0)
7296
}
7397

7498
static int darwin_transfer_status (struct usbi_transfer *itransfer, kern_return_t result) {
@@ -78,7 +102,7 @@ index 646c938..3900241 100644
78102
switch (result) {
79103
case kIOReturnUnderrun:
80104
case kIOReturnSuccess:
81-
@@ -1372,6 +1382,7 @@ static int darwin_transfer_status (struct usbi_transfer *itransfer, kern_return_
105+
@@ -1372,6 +1391,7 @@ static int darwin_transfer_status (struct usbi_transfer *itransfer, kern_return_
82106
return LIBUSB_TRANSFER_OVERFLOW;
83107
case kIOUSBTransactionTimeout:
84108
usbi_err (ITRANSFER_CTX (itransfer), "transfer error: timed out");
@@ -87,7 +111,7 @@ index 646c938..3900241 100644
87111
default:
88112
usbi_err (ITRANSFER_CTX (itransfer), "transfer error: %s (value = 0x%08x)", darwin_error_str (result), result);
89113
diff --git a/libusb/os/darwin_usb.h b/libusb/os/darwin_usb.h
90-
index a71d464..e3bdad8 100644
114+
index a71d464..4aadf8c 100644
91115
--- a/libusb/os/darwin_usb.h
92116
+++ b/libusb/os/darwin_usb.h
93117
@@ -139,6 +139,7 @@ struct darwin_device_handle_priv {
@@ -97,4 +121,12 @@ index a71d464..e3bdad8 100644
97121
+ UInt64 frames[256];
98122
uint8_t endpoint_addrs[USB_MAXENDPOINTS];
99123
} interfaces[USB_MAXINTERFACES];
100-
};
124+
};
125+
@@ -146,6 +147,7 @@ struct darwin_device_handle_priv {
126+
struct darwin_transfer_priv {
127+
/* Isoc */
128+
IOUSBIsocFrame *isoc_framelist;
129+
+ size_t num_iso_packets;
130+
131+
/* Control */
132+
#if !defined (LIBUSB_NO_TIMEOUT_DEVICE)

0 commit comments

Comments
 (0)