ftd2xx:调用 FT_GetDeviceInfoList() 时内存损坏

问题描述 投票:0回答:1

我正在使用 FTDI 提供的“ftd2xx”库在 Visual Studio 中编写 C++ 应用程序。

我尝试检索所有已连接设备的列表。所以我的应用程序看起来像这样:

// Application.cpp
int main()
{
    std::cout << "My FTDI finder!\n";

    FTDIManager ftdiManager;
    std::vector<FTDIDevice*> deviceList = ftdiManager.getDeviceList();
    ...
}

现在发生的令人讨厌的事情是,当调用

FT_GetDeviceInfoList(&devInfo, &index);
时,varialbe
numDevices
被覆盖!

怎么会发生这种事?

这是我的“getDeviceList”函数(位于头文件下方):

// ftdi.cpp
#include "ftdi.h"

std::vector<FTDIDevice*> FTDIManager::getDeviceList() {
    std::vector<FTDIDevice*> deviceList;

    FT_STATUS ftStatus;
    DWORD numDevices;
    ftStatus = FT_CreateDeviceInfoList(&numDevices);
    if (ftStatus != FT_OK) {
        std::cerr << "Error: FT_CreateDeviceInfoList failed with status " << ftStatus << std::endl;
        return deviceList;
    }

    for (DWORD i = 0; i < numDevices; ++i) {
        FT_DEVICE_LIST_INFO_NODE devInfo;

        ftStatus = FT_GetDeviceInfoList(&devInfo, &i);
        if (ftStatus == FT_OK) {
            std::string description = reinterpret_cast<const char*>(devInfo.Description);
            std::string serial = reinterpret_cast<const char*>(devInfo.SerialNumber);
            // Create specific device object based on description or other criteria if needed
            FTDIDevice* device = new SpecificFTDIDevice(description, serial);
            deviceList.push_back(device);
        }
    }

    return deviceList;
}
// ftdi.h
#include <ftd2xx.h>
#include <vector>

class FTDIDevice {
public:
    FTDIDevice(const std::string& description, const std::string& serial) : description(description), serial(serial) {}

    std::string getDescription() const { return description; }
    std::string getSerial() const { return serial; }

    virtual std::string getDeviceName() const = 0; // Pure virtual function to get device name

private:
    std::string description;
    std::string serial;
};


class SpecificFTDIDevice : public FTDIDevice {
public:
    SpecificFTDIDevice(const std::string& description, const std::string& serial) : FTDIDevice(description, serial) {}

    std::string getDeviceName() const override {
        // Implement device-specific functionality to get the name
        return "Specific Device Name";
    }
};


class FTDIManager {
public:
    FTDIManager() {}
    ~FTDIManager() {}

    std::vector<FTDIDevice*> getDeviceList();
};

感谢任何意见!

ftdi
1个回答
0
投票

来自 https://www.ftdichip.com/Support/Knowledgebase/index.html?ft_getdeviceinfolist.htm :

FT_STATUS FT_GetDeviceInfo (FT_DEVICE_LIST_INFO_NODE *pDest, LPDWORD lpdwNumDevs) 

Parameters
*pDest Pointer to an array of FT_DEVICE_LIST_INFO_NODE structures.
lpdwNumDevs Pointer to the number of elements in the array. 

示例指出:

// allocate storage for list based on numDevs
devInfo = (FT_DEVICE_LIST_INFO_NODE*)malloc(sizeof(FT_DEVICE_LIST_INFO_NODE)*numDevs);

// get the device information list
ftStatus = FT_GetDeviceInfoList(devInfo,&numDevs);

考虑到 devInfo 只有“一”长,FT_GetDeviceInfoList 的原始访问会简单地写入 devInfo 的长度。我的 C++ 有点生锈且过时(11 之前),但这应该可以解决问题:

// ftdi.cpp
#include "ftdi.h"

std::vector<std::unique_ptr<FTDIDevice>> FTDIManager::getDeviceList() {
    std::vector<std::unique_ptr<FTDIDevice>> deviceList; //maybe shared_ptr is the better choice here. Have not used c++ for a long time.

    FT_STATUS ftStatus;
    DWORD numDevices;
    ftStatus = FT_CreateDeviceInfoList(&numDevices);
    if (ftStatus != FT_OK) {
        std::cerr << "Error: FT_CreateDeviceInfoList failed with status " << ftStatus << std::endl;
        return deviceList;
    }

    std::vector<FT_DEVICE_LIST_INFO_NODE> devInfoArray(numDevices);
    ftStatus = FT_GetDeviceInfoList(devInfoArray.data(), &i);
    if (ftStatus == FT_OK) {
        for (auto &devInfo: devInfoArray) {
            // Create specific device object based on description or other criteria if needed
            auto device = std::make_unique<SpecificFTDIDevice>(devInfo.Description, devInfo.SerialNumber);
            deviceList.emplace_back(std::move(device));
        }
    }

    return std::move(deviceList);
}
© www.soinside.com 2019 - 2024. All rights reserved.