如何使用libusb正确释放设备列表? 我试图用VisualStudio 2019项目类型编写控制台应用程序,用C++语言编写一个代码,从USB设备类型CyPress FX3读写。我在程序中调用libusb_get_device_list一次,打开所需设备后,我立即调用libusb_free_device_list(devs,1)来释放列表。 我原以为这将释放所有设备,但在节目结束时,我在终端上收到一条消息说: libusb: warning [libusb_exit] some libusb_devices were leaked

如何使用libusb正确释放设备列表? 我试图用VisualStudio 2019项目类型编写控制台应用程序,用C++语言编写一个代码,从USB设备类型CyPress FX3读写。我在程序中调用libusb_get_device_list一次,打开所需设备后,我立即调用libusb_free_device_list(devs,1)来释放列表。 我原以为这将释放所有设备,但在节目结束时,我在终端上收到一条消息说: libusb: warning [libusb_exit] some libusb_devices were leaked,usb,libusb,libusb-1.0,Usb,Libusb,Libusb 1.0,我试着打印设备列表,但它是空的,我不把它称为函数get_device_list。我也试着寻找类似的问题,但我发现的几个问题对我没有帮助。如果有人能指出我做错了什么,并向我解释我应该如何释放这份清单,我将不胜感激 这是产生此错误的代码的主要部分: #include <iostream> #include "libusb.h" constexpr auto VID = 0x04B4; constexpr auto PID = 0x00F0; constexpr a

我试着打印设备列表,但它是空的,我不把它称为函数get_device_list。我也试着寻找类似的问题,但我发现的几个问题对我没有帮助。如果有人能指出我做错了什么,并向我解释我应该如何释放这份清单,我将不胜感激

这是产生此错误的代码的主要部分:

#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;
}
#包括
#包括“libusb.h”
constexpr auto VID=0x04B4;
constexpr自动PID=0x00F0;
constexpr auto OUT_ENDPOINT_ID=1;
constexpr auto IN_ENDPOINT_ID=1;
constexpr自动大小_的_数据包=4;
使用名称空间std;
结构libusb\u设备描述符DeviceDescriptor;
libusb_设备**devs//指向设备指针的指针,用于检索设备列表
libusb_context*context=NULL//libusb会话
libusb_device_handle*DeviceHandle=NULL//设备手柄
int main()
{
int-RetVal=libusb_init(&context);//初始化库会话
ssize_t NumberOfDevices;//保存列表中的设备数
libusb_set_debug(context,3);//按照文档中的建议,将详细级别设置为3
NumberOfDevices=libusb\u get\u device\u list(context,&devs);//获取设备列表
DeviceHandle=libusb\u open\u device\u with\u vid\u pid(context,vid,pid);//这些是Cypress FX3特定设备的供应商ID和产品ID
libusb_free_device_list(devs,1);//释放列表,取消释放其中的设备
unsigned char*DataOut=新的unsigned char[4];//要写入的数据
DataOut[0]=“a”;DataOut[1]=“b”;DataOut[2]=“c”;DataOut[3]=“d”;//一些伪值
int BytesWrite;//用于确定写入了多少字节
如果(libusb_内核_驱动程序_活动(DeviceHandle,0)=1){//查看是否连接了内核驱动程序

不欢迎使用Stackoverflow!这是一个很好的问题。函数调用中的'RetVal'变量有任何错误吗?我在这里一眼就看不到任何错误。非常感谢您的回答。我省略了我为RetVal编写的检查,因此代码将更清晰、更易于阅读,但'RetVal'变量中没有任何错误从函数调用