【发布时间】:2019-05-23 10:17:05
【问题描述】:
我正在从数据库中读取 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 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;
【问题讨论】:
-
*this可能是不存在的对象。 -
您没有混合调试和发布 DLL 或使用不同版本的 VC++ 编译的 DLL?
-
旁白:请不要这样做:
if (not client.has_value()),总是写if (!client.has_value()) -
此错误意味着您正在尝试访问尚未声明/设置的位置/内存。几天前我遇到了同样的错误,并通过检查未声明的内存来解决。
-
@DarkSorrow 没有办法检查某物是否是有效对象。
标签: c++ std access-violation unordered-map