Check validity of iterator before dereferencing

This commit is contained in:
Brandon Rothweiler 2019-08-13 18:00:12 -07:00
parent fdfa14d9cc
commit 8857db9833
1 changed files with 7 additions and 4 deletions

View File

@ -316,12 +316,15 @@ unsigned int NetPlayClient::OnData(sf::Packet& packet)
PlayerId pid;
packet >> pid;
INFO_LOG(NETPLAY, "Player %s (%d) left", m_players.find(pid)->second.name.c_str(), pid);
m_dialog->OnPlayerDisconnect(m_players.find(pid)->second.name);
{
std::lock_guard<std::recursive_mutex> lkp(m_crit.players);
const auto it = m_players.find(pid);
if (it == m_players.end())
break;
const auto& player = it->second;
INFO_LOG(NETPLAY, "Player %s (%d) left", player.name.c_str(), pid);
m_dialog->OnPlayerDisconnect(player.name);
m_players.erase(m_players.find(pid));
}