I'm trying to write a code to read and write from usb device type Cypress FX3 using Visual Studio 2019 project type is Console App, in c++ language. I call libusb_get_device_list one time during the program, and once I open the desired device I'm calling libusb_free_device_list(devs, 1) immediately to free the list. I thought this will free all devices, but at the end of the program I get a message in the terminal saying:
libusb: warning [libusb_exit] some libusb_devices were leaked
I tried printing the device list, but it was empty, and I don't call it the function get_device_list anywhere else. I also tried looking for similar questions, but the few I found didn't help me. I would be grateful if anyone could point out what am I doing wrong, and explain to me hoe should I free this list.
This is the main part of my code which produces this error:
#include <iostream>
#include "libusb.h"
constexpr auto VID = 0x04B4;
constexpr auto PID = 0x00F0;
constexpr auto OUT_ENDPOINT_ID = 1;
constexpr auto IN_ENDPOINT_ID = 1;
constexpr auto SIZE_OF_PACKET = 4;
using namespace std;
struct libusb_device_descriptor DeviceDescriptor;
libusb_device** devs; //pointer to pointer of device, used to retrieve a list of devices
libusb_context* context = NULL; //a libusb session
libusb_device_handle* DeviceHandle = NULL; //a device handle
int main()
{
int RetVal = libusb_init(&context); //initialize a library session
ssize_t NumberOfDevices; //holding number of devices in list
libusb_set_debug(context, 3); //set verbosity level to 3, as suggested in the documentation
NumberOfDevices = libusb_get_device_list(context, &devs); //get the list of devices
DeviceHandle = libusb_open_device_with_vid_pid(context, VID, PID); //these are vendorID and productID for Cypress FX3 specific device
libusb_free_device_list(devs, 1); //free the list, unref the devices in it
unsigned char* DataOut = new unsigned char[4]; //data to write
DataOut[0] = 'a'; DataOut[1] = 'b'; DataOut[2] = 'c'; DataOut[3] = 'd'; //some dummy values
int BytesWritten; //used to find out how many bytes were written
if (libusb_kernel_driver_active(DeviceHandle, 0) == 1) { //find out if kernel driver is attached
cout << "Kernel Driver Active" << endl;
if (libusb_detach_kernel_driver(DeviceHandle, 0) == 0) //detach it
cout << "Kernel Driver Detached!" << endl;
}
RetVal = libusb_claim_interface(DeviceHandle, 0); //claim interface 0 (the first) of device (desired device has only 1)
RetVal = libusb_bulk_transfer(DeviceHandle, (OUT_ENDPOINT_ID | LIBUSB_ENDPOINT_OUT), DataOut, SIZE_OF_PACKET, &BytesWritten, 0); //the out endpoint of current device is 1
unsigned char* DataIn = new unsigned char[4]; //data to write
int BytesRead;
RetVal = libusb_bulk_transfer(DeviceHandle, (IN_ENDPOINT_ID | LIBUSB_ENDPOINT_IN), DataIn, sizeof(DataIn), &BytesRead, 0);
if (RetVal == 0 && BytesRead == sizeof(DataIn)) {
// results of the transaction can now be found in the data buffer
// parse them here and report button press
cout << "Read Successful! Data is: " << DataIn[0] << DataIn[1] << DataIn[2] << DataIn[3] << endl;
}
else {
cout << "Read Error" << endl;
}
RetVal = libusb_release_interface(DeviceHandle, 0); //release the claimed interface
delete[] DataOut; //delete the allocated memory for data
delete[] DataIn; //delete the allocated memory for data
libusb_close(DeviceHandle); //close the device we opened
libusb_exit(context); //needs to be called at the end
return 0;
}
I omitted the checks that I've written for RetVal so the code will be cleaner and easier to read, when I run thew program I don't have any errors in 'RetVal' variable from function calls.
I'm new here, First time I'm posting. I've read the section "How do I ask a good question?" so I hope I have written it properly. please help me out if I'm doing anything wrong.
Thank you very much.