From 706ef442c17df7927adce46a6e272f989032a614 Mon Sep 17 00:00:00 2001 From: bash <31279994+hermensbas@users.noreply.github.com> Date: Tue, 12 Aug 2025 00:12:51 +0200 Subject: [PATCH] Fix another C0000005 ACCESS_VIOLATION https://github.com/liyunfan1223/mod-playerbots/issues/1537 --- src/PlayerbotMgr.cpp | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/src/PlayerbotMgr.cpp b/src/PlayerbotMgr.cpp index dee1437c..e4fda847 100644 --- a/src/PlayerbotMgr.cpp +++ b/src/PlayerbotMgr.cpp @@ -222,20 +222,39 @@ void PlayerbotHolder::HandlePlayerBotLoginCallback(PlayerbotLoginQueryHolder con void PlayerbotHolder::UpdateSessions() { - for (PlayerBotMap::const_iterator itr = GetPlayerBotsBegin(); itr != GetPlayerBotsEnd(); ++itr) + // snapshot of keys + std::vector guids; + guids.reserve(playerBots.size()); + for (auto const& kv : playerBots) + guids.push_back(kv.first); + + // safe iterate of snapshot of keys. + for (ObjectGuid const& guid : guids) { - Player* const bot = itr->second; + Player* bot = GetPlayerBot(guid); + if (!bot) + continue; + if (bot->IsBeingTeleported()) { - PlayerbotAI* botAI = GET_PLAYERBOT_AI(bot); - if (botAI) + if (PlayerbotAI* botAI = GET_PLAYERBOT_AI(bot)) { botAI->HandleTeleportAck(); + + // Don’t process packets in the same tick as a teleport ack + continue; } } - else if (bot->IsInWorld()) + + if (!bot->IsInWorld()) { - HandleBotPackets(bot->GetSession()); + continue; + } + + if (WorldSession* sess = bot->GetSession()) + { + // This may log the bot out or mutate the map, hence the usage of a snapshop + HandleBotPackets(sess); } } }