c++ - 向 std::unordered_map 插入数据时发生访问冲突

标签 c++ std access-violation unordered-map

我正在从数据库读取 plc 的读/写配置并将它们存储在 std::unordered_map 中。

每当我尝试将提取的记录插入 std::unordered_map 时,我都会遇到异常。在插入语句显示数据之前分析变量是 可与变量一起使用。

我无法理解为什么在 SeimensPLC.exe 中的 0x00007FFC0C05BD82 (snap7.dll) 抛出异常。

代码:

//plc_ip         tuple(plc_client_object    read_vector    write_vector)
std::unordered_map<std::string, plc_common::plc_config_data_tuple> plc_;

std::optional<TS7Client> plc::connect_plc(const std::string& ip, std::uint8_t connectionType, std::uint16_t rack, std::uint16_t slot)
{
    TS7Client client;
    client.SetConnectionType(connectionType);   //PG-PC : Programming Console type connection
    if (client.ConnectTo(ip.c_str(), rack, slot) not_eq EXIT_SUCCESS)
        return std::nullopt;
    return client;
}

bool plc::add_plc(const std::string& ip, const std::vector<config_table_struct>& config_list)
{
    try
    {
        std::optional<TS7Client> client = this->connect_plc(ip);
        if (not client.has_value())
        {
            std::string strErr = fmt::format("Unable to connect to plc : {}", ip);          
            LOG_ERROR << strErr;
            return false;
        }
        plc_common::read_vector         read_vector;
        plc_common::write_vector        write_vector;
        std::for_each(config_list.begin(), config_list.end(), [&](const config_table_struct& config_struct) {

            plc_common::config_struct   config;
            config.serial_no        = config_struct.serial_no;
            config.area_number      = config_struct.area_number;
            config.read_location    = config_struct.read_location;
            config.read_length      = config_struct.read_length;
            config.scan_rate        = config_struct.scan_rate;
            config.data_type        = config_struct.data_type;
            config.area_type        = 0x84;
            config.data_queue       = plc_common::data_queue{};
            read_vector.push_back(config);
        });
        plc_common::plc_config_data_tuple plc_config_data_tuple = std::make_tuple(client.value(), read_vector, write_vector);
        this->plc_.insert(std::make_pair(ip, plc_config_data_tuple)); //Executing this statement gives exception
        return true;
    }
    catch (const std::exception& ex)
    {       
        LOG_FATAL << "Exception : " << ex.what();
    }
    return false;
}

异常:

Exception thrown at 0x00007FFC0C05BD82 (snap7.dll) in SeimensPLC.exe: 0xC0000005: Access violation reading location 0xFFFFFFFFFFFFFF04.

Unhandled exception at 0x00007FFC0C05BD82 (snap7.dll) in SeimensPLC.exe: 0xC0000005: Access violation reading location 0xFFFFFFFFFFFFFF04.

编辑:

根据要求,我添加了所有 typedef。

typedef std::variant<bool, std::uint8_t, std::int16_t, std::int32_t, std::uint16_t, std::uint32_t, std::float_t, char> plc_data_type;
typedef std::queue<plc_common::plc_data_type>                       data_queue;
typedef struct {
    std::uint32_t           serial_no;
    std::uint8_t            area_type;
    std::uint8_t            area_number;
    std::uint16_t           read_location;
    std::uint16_t           read_length;
    std::string             data_type;
    std::uint32_t           scan_rate;
    plc_common::data_queue  data_queue;
} config_struct;
//read_vector/write_vector      area_type    area_number    read/write_location   read/write_size     scanRate     read/write_queue   
typedef std::vector<plc_common::config_struct> read_vector, write_vector;
//                   s7_socket    vector storing read/write information
typedef std::tuple<TS7Client, plc_common::read_vector, plc_common::write_vector> plc_config_data_tuple;

最佳答案

I am unable to understand why the exception is thrown at 0x00007FFC0C05BD82

你应该知道这不是抛出 C++ 异常,而是 access violation由具有内存保护的硬件引发。这可能有很多原因。最常见的原因之一是调用 Undefined Behavior代码中的某处随后出现内存损坏或访问通常不应访问的内存。

至于怎么调试,可以用Dr. Memory如果您使用的是 Linux,则在 Windows 或 Valgrind 上。

关于c++ - 向 std::unordered_map 插入数据时发生访问冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56273050/

相关文章:

c++ - pthreads 中的内存模型规范

c++ - 访问冲突是什么意思?

c++ - 为什么更改 visual studio 版本后我的函数会崩溃?

delphi - 在这种情况下,为什么需要在调用 AviFileExit() 之前取消 IAviFile 指针?

c++ - 当按值发送参数时,为什么不调用默认的复制构造函数?

c++ - 尝试使用 Crypto++ RSA 方案加密文件时崩溃

c++ - 将 std 字符串转换为 const char*

c++ - std::ofstream 无法在 win7/64 和 msvc2013 上使用 std::ios::ate 打开大文件

c++ - std::uncaught_exception 和 Microsoft __uncaught_exception 之间的区别?

c++ - 用零初始化 std::chrono::time_point 变量