我正在尝试使用"MUD游戏编程";我正在研究这些例子,但当我试图从向量中删除连接时,我得到了一个错误:";中止(堆芯转储("这通常发生在从矢量中擦除最后一个时。我尝试过一些解决方案,比如检查迭代器!=删除之前使用connlist.end((,如果列表中只剩下一个元素,则尝试使用connlist.clear((,但这些似乎没有帮助。
我正在使用g++来编译和处理Cygwin。
错误发生在connlist.esers((,您可以看到当用户键入";退出">
void NMudServer::StartListening() {
fd_set read_set;
std::vector<SocketLib::Connection>::iterator itr;
std::vector<SocketLib::Connection>::iterator itr2;
TIMEVAL zerotime;
zerotime.tv_usec = 0;
zerotime.tv_sec = 0;
char buffer[buf_len];
int err;
lsock.Listen( default_port );
if( lsock.IsListening() ) {
std::cout << "Telnet listening on port " << default_port << "." << std::endl;
} else {
std::cout << "Could not start Telnet listening socket! - Last error code: " << WSAGetLastError() << std::endl;
return;
}
while( lsock.IsListening() ) {
FD_ZERO( &read_set );
FD_SET( lsock.GetSock(), &read_set );
for( itr = connlist.begin(); itr != connlist.end(); ++itr ) {
FD_SET( itr->GetSock(), &read_set );
}
int sel = select( 0x7FFFFFFF, &read_set, NULL, NULL, &zerotime );
if( sel > 0 ) {
if( FD_ISSET( lsock.GetSock(), &read_set ) ) {
SocketLib::DataSocket dsock = lsock.Accept();
SocketLib::Connection conn( dsock, buf_len );
connlist.push_back( conn );
conn.Send( "Hello!rn", 8 );
}
for( itr = connlist.begin(); itr != connlist.end(); ++itr ) {
if( FD_ISSET( itr->GetSock(), &read_set ) ) {
err = itr->Receive();
if( err == -1 ) {
std::cout << "Socket receiving error!" << std::endl;
std::cout << "Error code: " << WSAGetLastError() << std::endl;
std::cout << "Exiting due to error." << std::endl;
CloseAllConnections(); // This is when the connection is closed. Need to only close the one connection.
break;
} else if( err == 0 ) {
itr->Close();
connlist.erase( itr );
--itr;
} else if( itr->IsReady() ) {
int size;
size = itr->GetData( buffer );
if( strcmp( buffer, "servquitrn" ) == 0 ) {
CloseAllConnections();
} else if( strcmp( buffer, "quitrn" ) == 0 ) {
itr->Close();
connlist.erase( itr ); // When you go to erase the last element, it errors
--itr;
} else {
// Echo back the data to all connections
for( itr2 = connlist.begin(); itr2 != connlist.end(); ++itr2 ) {
if( itr2->GetSock() != itr->GetSock() ) {
int err2;
itr2->Send( buffer, size );
if( err2 == -1 ) {
std::cout << "Socket sending error: " << WSAGetLastError() << std::endl;
}
} else {
itr2->Send( "rn", 2 );
}
} // end for
}// end if-else
itr->Reset();
} // end if-else-else
} // end if
} // end for
} // end if sel
} // end while
}
void NMudServer::CloseAllConnections() {
lsock.Close();
std::vector<SocketLib::Connection>::iterator itr;
for( itr = connlist.begin(); itr != connlist.end(); ++itr ) {
itr->Close();
}
WSACleanup();
}
作为参考,这本书是:彭顿,罗恩MUD游戏编程波士顿,马萨诸塞州,美国:课程技术/圣智学习,2003。
您正在迭代一个向量,并在循环中从中擦除元素。这从来都不是个好主意。vector::erase
会使用于进一步迭代的迭代器无效。
请参阅:https://en.cppreference.com/w/cpp/container/vector/erase
原因是当您擦除元素时,向量可能会重新分配和/或移动元素。
可能的解决方案:
只需关闭环路内的连接,然后执行擦除删除:
connlist.erase(std::remove_if(connlist.begin(), connlist.end(), is_closed), connlist.end());
is_closed
类似于
is_closed = [](const SocketLib::Connection& c) {return !c.open();};
PS:我不知道SocketLib
,所以你可能会把is_closed
改成
PPS:基于使用范围的循环,如for(auto& connection : connlist) {...}
编辑:
正如评论中所提到的,在这种情况下,矢量不会重新分配,其他点仍然有效。