@@ -172,7 +172,8 @@ static struct hid_hotplug_context {
172172 /* Linked list of the hotplug callbacks */
173173 struct hid_hotplug_callback * hotplug_cbs ;
174174
175- /* Linked list of the device infos (mandatory when the device is disconnected) */
175+ /* Linked list of the device infos (mandatory when the device is disconnected).
176+ * Only accessed from callback_thread. */
176177 struct hid_device_info * devs ;
177178} hid_hotplug_context = {
178179 .next_handle = FIRST_HOTPLUG_CALLBACK_HANDLE ,
@@ -1216,7 +1217,10 @@ static void* callback_thread(void* user_data)
12161217 (void ) user_data ;
12171218
12181219 hidapi_thread_mutex_lock (& hid_hotplug_context .callback_thread );
1219-
1220+
1221+ /* Initialize the known-device list; only accessed from this thread */
1222+ hid_hotplug_context .devs = hid_enumerate (0 , 0 );
1223+
12201224 /* We stop the thread if by the moment there are no events left in the queue there are no callbacks left */
12211225 while (1 ) {
12221226 /* Wait for events to arrive or shutdown signal */
@@ -1338,14 +1342,12 @@ int HID_API_EXPORT HID_API_CALL hid_hotplug_register_callback(unsigned short ven
13381342 last -> next = hotplug_cb ;
13391343 }
13401344 else {
1341- /* Fill already connected devices so we can use this info in disconnection notification */
13421345 if (libusb_init (& hid_hotplug_context .context )) {
13431346 free (hotplug_cb );
13441347 pthread_mutex_unlock (& hid_hotplug_context .mutex );
13451348 return -1 ;
13461349 }
13471350
1348- hid_hotplug_context .devs = hid_enumerate (0 , 0 );
13491351 hid_hotplug_context .hotplug_cbs = hotplug_cb ;
13501352
13511353 /* Arm a global callback to receive ALL notifications for HID class devices */
@@ -1370,7 +1372,8 @@ int HID_API_EXPORT HID_API_CALL hid_hotplug_register_callback(unsigned short ven
13701372 hid_hotplug_context .mutex_in_use = 1 ;
13711373
13721374 if ((flags & HID_API_HOTPLUG_ENUMERATE ) && (events & HID_API_HOTPLUG_EVENT_DEVICE_ARRIVED )) {
1373- struct hid_device_info * device = hid_hotplug_context .devs ;
1375+ struct hid_device_info * all_devs = hid_enumerate (0 , 0 );
1376+ struct hid_device_info * device = all_devs ;
13741377 /* Notify about already connected devices, if asked so */
13751378 while (device != NULL ) {
13761379 if (hid_internal_match_device_id (device -> vendor_id , device -> product_id , hotplug_cb -> vendor_id , hotplug_cb -> product_id )) {
@@ -1379,6 +1382,7 @@ int HID_API_EXPORT HID_API_CALL hid_hotplug_register_callback(unsigned short ven
13791382
13801383 device = device -> next ;
13811384 }
1385+ hid_free_enumeration (all_devs );
13821386 }
13831387
13841388 hid_hotplug_context .mutex_in_use = old_state ;
0 commit comments