SIGSEGV,分段错误。仅适用于 linux,适用于 windows
SIGSEGV, Segmentation fault. Only on linux, it works on windows
我已经坚持了几个小时,出于某种原因,我只在 Linux 上收到分段错误。它只发生在长度为 17 个或更多字符的数据包中。我正在使用 Enet 库来发送数据包。任何人都可以帮助我,我不知道我在这里错过了什么......
这是gdb输出的截图
这是服务器端示例:
void GameServer::start()
{
if (!m_isRunning)
m_isRunning = true;
ENetEvent event;
//ENetPacket *packet;
while (m_isRunning)
{
while (enet_host_service(server, &event, 1000) > 0)
{
switch (event.type)
{
case ENET_EVENT_TYPE_CONNECT:
std::cout << "A new client connected from ";
std::cout << event.peer->address.host << " ";
std::cout << event.peer->address.port << std::endl;
break;
case ENET_EVENT_TYPE_DISCONNECT:
printf("%s disconnected.\n", event.peer->data);
delete (char*)event.peer->data;
break;
case ENET_EVENT_TYPE_RECEIVE:
{
std::cout << "A packet of length ";
std::cout << event.packet->dataLength;
std::cout << " containing ";
std::cout << event.packet->data;
std::cout << " was received from ";
printf("%s", event.peer->data);
std::cout << " on channel ";
std::cout << event.channelID << std::endl;
// Split the packet data, ex: packettype:data:data
std::vector<std::string> tokens;
std::string data;
std::istringstream split((const char *)event.packet->data);
// Process the packet data
while (std::getline(split, data, ':')) Line 89: Crash here
tokens.push_back(data);
if (tokens.size() > 1)
{
//pc = player connected so we will broadcast the name to all peers
if (tokens[0] == "pc")
{
if (tokens.size() == 2)
{
char* data = new char[tokens[1].length() + 1];
strcpy(data, tokens[1].c_str());
event.peer->data = (void*)data;
for (size_t i = 0; i < server->connectedPeers; i++)
{
enet_host_broadcast(server, 0, event.packet);
}
}
}
//ma = message all so we are going to broadcast the message to all peers
else if (tokens[0] == "ma")
{
if (tokens.size() == 2)
{
enet_host_broadcast(server, 0, event.packet);
}
}
/* One could just use enet_host_service() instead. */
enet_host_flush(server);
}
/* Clean up the packet now that we're done using it. */
enet_packet_destroy(event.packet);
break;
}
case ENET_EVENT_TYPE_NONE:
break;
default:
break;
}
}
}
}
这是客户端创建数据包然后发送到服务器的过程。
if (connected) {
std::cout << "Input: ";
std::string str = "";
std::getline(std::cin, str);
if (str.length() == 0) { continue; }
packet = enet_packet_create(str.c_str(), str.length() + 1, ENET_PACKET_FLAG_RELIABLE);
enet_peer_send(peer, 0, packet);
}
如果代码示例不够我可以提供更多。
malloc()
中的崩溃通常意味着有人在 早些时候 的某个时间写入了已分配缓冲区的末尾。缓冲区溢出损坏了存储在空闲内存中的指向 link 个空闲块的指针。直到 malloc()
尝试使用这些指针才导致问题。然后砰!
Post event.packet 类型的声明。您确定 data[] 对于您放入其中的数据足够大吗?
我已经坚持了几个小时,出于某种原因,我只在 Linux 上收到分段错误。它只发生在长度为 17 个或更多字符的数据包中。我正在使用 Enet 库来发送数据包。任何人都可以帮助我,我不知道我在这里错过了什么......
这是gdb输出的截图
void GameServer::start()
{
if (!m_isRunning)
m_isRunning = true;
ENetEvent event;
//ENetPacket *packet;
while (m_isRunning)
{
while (enet_host_service(server, &event, 1000) > 0)
{
switch (event.type)
{
case ENET_EVENT_TYPE_CONNECT:
std::cout << "A new client connected from ";
std::cout << event.peer->address.host << " ";
std::cout << event.peer->address.port << std::endl;
break;
case ENET_EVENT_TYPE_DISCONNECT:
printf("%s disconnected.\n", event.peer->data);
delete (char*)event.peer->data;
break;
case ENET_EVENT_TYPE_RECEIVE:
{
std::cout << "A packet of length ";
std::cout << event.packet->dataLength;
std::cout << " containing ";
std::cout << event.packet->data;
std::cout << " was received from ";
printf("%s", event.peer->data);
std::cout << " on channel ";
std::cout << event.channelID << std::endl;
// Split the packet data, ex: packettype:data:data
std::vector<std::string> tokens;
std::string data;
std::istringstream split((const char *)event.packet->data);
// Process the packet data
while (std::getline(split, data, ':')) Line 89: Crash here
tokens.push_back(data);
if (tokens.size() > 1)
{
//pc = player connected so we will broadcast the name to all peers
if (tokens[0] == "pc")
{
if (tokens.size() == 2)
{
char* data = new char[tokens[1].length() + 1];
strcpy(data, tokens[1].c_str());
event.peer->data = (void*)data;
for (size_t i = 0; i < server->connectedPeers; i++)
{
enet_host_broadcast(server, 0, event.packet);
}
}
}
//ma = message all so we are going to broadcast the message to all peers
else if (tokens[0] == "ma")
{
if (tokens.size() == 2)
{
enet_host_broadcast(server, 0, event.packet);
}
}
/* One could just use enet_host_service() instead. */
enet_host_flush(server);
}
/* Clean up the packet now that we're done using it. */
enet_packet_destroy(event.packet);
break;
}
case ENET_EVENT_TYPE_NONE:
break;
default:
break;
}
}
}
}
这是客户端创建数据包然后发送到服务器的过程。
if (connected) {
std::cout << "Input: ";
std::string str = "";
std::getline(std::cin, str);
if (str.length() == 0) { continue; }
packet = enet_packet_create(str.c_str(), str.length() + 1, ENET_PACKET_FLAG_RELIABLE);
enet_peer_send(peer, 0, packet);
}
如果代码示例不够我可以提供更多。
malloc()
中的崩溃通常意味着有人在 早些时候 的某个时间写入了已分配缓冲区的末尾。缓冲区溢出损坏了存储在空闲内存中的指向 link 个空闲块的指针。直到 malloc()
尝试使用这些指针才导致问题。然后砰!
Post event.packet 类型的声明。您确定 data[] 对于您放入其中的数据足够大吗?