c++ - libusb_get_device_list seg fault -


i writing file explorer application in qt c++ , have libusb function (qlist usbdevice::getdevicelist()) gets attached usb devices, checks each 1 products vendor , product id's, claims them , returns them in array. works fine , device want, have added refresh button should update device list shown in drop-down list (it calls getdevicelist function again) seg faults when calling:

int numdevices = libusb_get_device_list(null, &usbdevices); 

the second time around , can't life of me see why. if check on code below , see if have missed stupid helpful.

qlist<usbdevice*> usbdevice::getdevicelist() {  unsigned char manf[256] = {'\0'};  qlist<usbdevice*> usbdevicelist;  libusb_device **usbdevices; struct libusb_device_descriptor desc;  int numdevices = libusb_get_device_list(null, &usbdevices); if(numdevices < 0) {     libusb_free_device_list(usbdevices, 1);     return usbdevicelist; } qstring error;  for(int i=0; i!=numdevices; ++i) {     libusb_device *dev = usbdevices[i];     libusb_get_device_descriptor(dev, &desc);      if((desc.idvendor != vendorusbid) && (desc.idproduct != productusbid))         continue;      libusb_device_handle *handle = null;     libusb_config_descriptor *conf_desc = null;      int result = 0;      result = libusb_open(dev, &handle);     if(result < 0)     {         if(result == -3)         {          }         error = qstring(libusb_error_name(result));         continue;     }      int config = 1;        if( handle == null)     {         continue;     }      result = libusb_set_configuration(handle, config);     if(result < 0)     {         error = qstring(libusb_error_name(result));         continue;     }      result = libusb_get_config_descriptor(dev, 0, &conf_desc);     if(result < 0)     {         error = qstring(libusb_error_name(result));         continue;     }      result = libusb_claim_interface(handle, 0);     if(result < 0)     {         error = qstring(libusb_error_name(result));         continue;     }      result = libusb_get_string_descriptor_ascii(handle, desc.iproduct, manf, sizeof(manf));     if(result < 0)     {         error = qstring(libusb_error_name(result));         continue;     }      usbdevice *newdevice = new usbdevice();     newdevice->setdevicename(qstring((char*)manf));     newdevice->sethandle(handle);      usbdevicelist << newdevice; }  libusb_free_device_list(usbdevices, 1);  return usbdevicelist;  } 

you calling libusb_init() @ beginning of program, calling libusb_exit() @ beginning : before calling a.exec().

your first call happens in mainwindow constructor ?

you instead subclass qapplication, call libusb_init() in constructor , libusb_exit() in destructor.


Comments

Popular posts from this blog

c++ - OpenCV Error: Assertion failed <scn == 3 ::scn == 4> in unknown function, -

php - render data via PDO::FETCH_FUNC vs loop -

The canvas has been tainted by cross-origin data in chrome only -