@@ -1103,8 +1103,10 @@ static int init_device(struct libusb_device* dev, struct libusb_device* parent_d
11031103 if (tmp_dev -> bus_number != 0 ) {
11041104 usbi_dbg ("got bus number from ancestor #%d" , i );
11051105 parent_dev -> bus_number = tmp_dev -> bus_number ;
1106+ libusb_unref_device (tmp_dev );
11061107 break ;
11071108 }
1109+ libusb_unref_device (tmp_dev );
11081110 }
11091111 }
11101112 if (parent_dev -> bus_number == 0 ) {
@@ -1116,7 +1118,7 @@ static int init_device(struct libusb_device* dev, struct libusb_device* parent_d
11161118 dev -> port_number = port_number ;
11171119 priv -> depth = parent_priv -> depth + 1 ;
11181120 priv -> parent_dev = parent_dev ;
1119- dev -> parent_dev = libusb_ref_device ( parent_dev ) ;
1121+ dev -> parent_dev = parent_dev ;
11201122
11211123 // If the device address is already set, we can stop here
11221124 if (dev -> device_address != 0 ) {
@@ -1537,6 +1539,7 @@ static int windows_get_device_list(struct libusb_context *ctx, struct discovered
15371539 parent_priv = _device_priv (parent_dev );
15381540 // virtual USB devices are also listed during GEN - don't process these yet
15391541 if ( (pass == GEN_PASS ) && (parent_priv -> apib -> id != USB_API_HUB ) ) {
1542+ libusb_unref_device (parent_dev );
15401543 continue ;
15411544 }
15421545 break ;
@@ -1559,20 +1562,20 @@ static int windows_get_device_list(struct libusb_context *ctx, struct discovered
15591562 LOOP_BREAK (LIBUSB_ERROR_NO_MEM );
15601563 }
15611564 windows_device_priv_init (dev );
1562- // Keep track of devices that need unref
1563- unref_list [unref_cur ++ ] = dev ;
1564- if (unref_cur >= unref_size ) {
1565- unref_size += 64 ;
1566- unref_list = usbi_reallocf (unref_list , unref_size * sizeof (libusb_device * ));
1567- if (unref_list == NULL ) {
1568- usbi_err (ctx , "could not realloc list for unref - aborting." );
1569- LOOP_BREAK (LIBUSB_ERROR_NO_MEM );
1570- }
1571- }
15721565 } else {
15731566 usbi_dbg ("found existing device for session [%X] (%d.%d)" ,
15741567 session_id , dev -> bus_number , dev -> device_address );
15751568 }
1569+ // Keep track of devices that need unref
1570+ unref_list [unref_cur ++ ] = dev ;
1571+ if (unref_cur >= unref_size ) {
1572+ unref_size += 64 ;
1573+ unref_list = usbi_reallocf (unref_list , unref_size * sizeof (libusb_device * ));
1574+ if (unref_list == NULL ) {
1575+ usbi_err (ctx , "could not realloc list for unref - aborting." );
1576+ LOOP_BREAK (LIBUSB_ERROR_NO_MEM );
1577+ }
1578+ }
15761579 priv = _device_priv (dev );
15771580 }
15781581
@@ -1658,6 +1661,7 @@ static int windows_get_device_list(struct libusb_context *ctx, struct discovered
16581661 break ;
16591662 }
16601663 }
1664+ libusb_unref_device (parent_dev );
16611665 break ;
16621666 }
16631667 }
0 commit comments