diff --git a/hgdriver/hgdev/usb_manager.cpp b/hgdriver/hgdev/usb_manager.cpp index efb385f..887fd76 100644 --- a/hgdriver/hgdev/usb_manager.cpp +++ b/hgdriver/hgdev/usb_manager.cpp @@ -70,7 +70,7 @@ int LIBUSB_CALL usb_manager::usb_pnp_callback(libusb_context* ctx, libusb_device usb_manager* obj = (usb_manager*)monitor; // if (event == LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED) - libusb_ref_device(device); // keep the object until handle it + //libusb_ref_device(device); // keep the object until handle it //else if(event == LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT) // libusb_unref_device(device); @@ -187,17 +187,22 @@ void usb_manager::thread_notify_usb_event() } } + libusb_ref_device(pd.dev); // for re-enter the queue pnp_events_ notify_usb_event(pd, &retry); if (retry) { - if(std::chrono::duration_cast(std::chrono::system_clock::now() - pd.happen_time).count() + if (std::chrono::duration_cast(std::chrono::system_clock::now() - pd.happen_time).count() <= 5000) pnp_events_.Put(pd, sizeof(pd)); + else + retry = false; if(pnp_events_.Size() == 1) this_thread::sleep_for(chrono::milliseconds(1000)); else this_thread::sleep_for(chrono::milliseconds(delay)); } + if (!retry) + libusb_unref_device(pd.dev); } } } @@ -283,6 +288,7 @@ int usb_manager::on_usb_pnp_event(libusb_context *ctx, libusb_device *device, li PNPDEV pd; unsigned ms = std::chrono::duration_cast(std::chrono::system_clock::now() - born_).count(); + libusb_ref_device(device); pd.ctx = ctx; pd.dev = device; pd.event = event; @@ -295,9 +301,12 @@ int usb_manager::on_usb_pnp_event(libusb_context *ctx, libusb_device *device, li else { bool retry = false; + libusb_ref_device(device); notify_usb_event(pd, &retry); if(retry) pnp_events_.Put(pd, sizeof(pd)); + else + libusb_unref_device(device); } return 0;