简体   繁体   English

替换std :: map值时崩溃

[英]Crash when replacing std::map value

I have this map 我有这张地图

std::map<IPv4Address,std::vector<Agent>> report;

Where agent is defined as the following 代理定义如下

typedef struct
{
    IPv4Address Address;
    AgentType Type;
    std::map <IPv4Address, IPRoute> RoutingTable;
    MetricType metric;
    int UpdateReceived = 0;
}Agent;

I am sending this Agent struct through tcp sockets and saving the values in the report std::map 我正在通过tcp sockets发送此Agent结构,并将值保存在报告std::map

int receive = recv(as.socket, (void *) &agent, sizeof(agent),0);

The routing table is initially empty. 路由表最初是空的。 When the routing table size becomes >=1 the app crashes when adding to the map as seen below: 当路由表大小变为> = 1时,应用程序在添加到地图时会崩溃,如下所示:

             mutex.lock();
             PrintInfo("Mutex locked");
             if(report.find(as.ip) != report.end())
             {
                 //f tells us if the agent was connected before to the router
                 bool f = false;
                 std::vector<Agent> tmpv =report[as.ip];
                 int tmp;

                 PrintInfo("Vector loop");
                 for(std::size_t i=0 ; i < tmpv.size() ; i++)
                 {
                     if(tmpv[i].Type == agent.Type)
                     {
                         f = true;
                         tmp = i;
                         break;
                     }
                 }

                 PrintInfo("Vector loop End");

                 if(f)
                 {
                     PrintInfo("Found -> Replacing");
  --> This line crashes  report[as.ip][tmp] = agent;
                 }
                 else
                 {
                     PrintInfo("Not Found -> Adding");
                     report[as.ip].push_back(agent);
                 }


                 PrintInfo("After add");
             }

After serializing the struct using boost library everything worked fine. 使用boost库序列化结构后,一切工作正常。

Here is an example of serialization : 这是序列化的示例:

Struct : 结构:

typedef struct
{
    template<class Archive>
    void serialize(Archive &ar,const unsigned int version)
    {
        ar & Address & Type & InterceptionDuration & RoutingTable & metric & UpdateReceived;
    }

    IPv4Address Address;
    AgentType Type;
    double InterceptionDuration;
    std::map <IPv4Address, IPRoute> RoutingTable;
    MetricType metric;
    int UpdateReceived = 0;
}Agent;

Serializing : 序列化:

std::string SerializeAgent(Agent a)
{
    std::ostringstream archive_stream;
    boost::archive::text_oarchive archive(archive_stream);
    archive << a;
    std::string s = archive_stream.str();
    return s;
}

Deserializing : 反序列化:

Agent DeserializeAgent(std::string s)
{
        Agent a;
        std::string r(&s[0],s.size());
        std::istringstream archive_stream(r);
        boost::archive::text_iarchive archive(archive_stream);
        archive >> a;
        return a;
}

Sending through socket : 通过套接字发送:

std::string s = SerializeAgent(agent);
send(reporterSendingSocket,s.c_str(),s.size(),0);

Receiving through socket : 通过插座接收:

std::vector<char> response(REPORT_MAX_BUFFER_SIZE);

int receive = recv(as.socket, (void *) &response[0], response.size(),0);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM