C++ 替换std::map值时崩溃
我有这张地图C++ 替换std::map值时崩溃,c++,sockets,c++11,struct,stdmap,C++,Sockets,C++11,Struct,Stdmap,我有这张地图 std::map<IPv4Address,std::vector<Agent>> report; 路由表最初为空。当路由表大小变为>=1时,应用程序在添加到地图时崩溃,如下所示: mutex.lock(); PrintInfo("Mutex locked"); if(report.find(as.ip) != report.end()) {
std::map<IPv4Address,std::vector<Agent>> report;
路由表最初为空。当路由表大小变为>=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");
}
mutex.lock();
PrintInfo(“互斥锁”);
if(report.find(as.ip)!=report.end()
{
//f告诉我们代理之前是否连接到路由器
bool f=假;
std::vector tmpv=报告[as.ip];
int tmp;
PrintInfo(“向量循环”);
对于(std::size_t i=0;i替换”);
-->此行崩溃报告[as.ip][tmp]=代理;
}
其他的
{
PrintInfo(“未找到->添加”);
报告[as.ip]。推回(代理);
}
PrintInfo(“添加后”);
}
使用boost
库序列化结构后,一切正常
以下是序列化的一个示例:
结构:
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;
通过套接字发送:
std::string s = SerializeAgent(agent);
send(reporterSendingSocket,s.c_str(),s.size(),0);
通过插座接收:
std::vector<char> response(REPORT_MAX_BUFFER_SIZE);
int receive = recv(as.socket, (void *) &response[0], response.size(),0);
std::向量响应(报告最大缓冲区大小);
int receive=recv(as.socket,(void*)&response[0],response.size(),0);
不能通过网络发送包含指针的复杂数据。甚至不能本地连接到同一程序的另一个进程。指针(如映射中的指针)仅对当前进程是本地的。你需要修改数据结构。“->这行崩溃了”:崩溃到底是什么?@Someprogrammerdude我会尝试序列化,然后用c写下发生了什么
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;
}
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;
}
std::string s = SerializeAgent(agent);
send(reporterSendingSocket,s.c_str(),s.size(),0);
std::vector<char> response(REPORT_MAX_BUFFER_SIZE);
int receive = recv(as.socket, (void *) &response[0], response.size(),0);