From cc9da8374447dc52d0212cd247df0b1b274c84c6 Mon Sep 17 00:00:00 2001 From: agatho Date: Tue, 3 Feb 2026 20:56:12 +0100 Subject: [PATCH] fix(botai): Use cached GUID in destructor to prevent dangling pointer crash BotAI::~BotAI() was accessing _bot->GetGUID() for blackboard cleanup, but during destruction _bot may be a dangling pointer to already-freed memory. This caused ACCESS_VIOLATION crashes (C0000005) during bot session destruction. The fix uses _cachedBotGuid (captured in constructor) instead, following the same safe pattern already used in UnsubscribeFromEventBuses(). Root cause: Race condition where Player object is destroyed before BotAI destructor completes blackboard cleanup. Evidence from crash logs: - "Removed bot blackboard for Player-1-214CB312020" (hex pointer value) - "AFKSimulator::OnShutdown - Bot AFK simulator shutdown" (empty name) Co-Authored-By: Claude Opus 4.5 Signed-off-by: luis --- src/modules/Playerbot/AI/BotAI.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/modules/Playerbot/AI/BotAI.cpp b/src/modules/Playerbot/AI/BotAI.cpp index e663938d0..8ba00cb27 100644 --- a/src/modules/Playerbot/AI/BotAI.cpp +++ b/src/modules/Playerbot/AI/BotAI.cpp @@ -329,9 +329,12 @@ BotAI::~BotAI() // garbage data, causing std::bad_alloc when string tries huge allocation. // Phase 4: Cleanup Shared Blackboard - if (_sharedBlackboard && _bot) + // CRITICAL FIX: Use _cachedBotGuid instead of _bot->GetGUID()! + // During destructor, _bot may be a dangling pointer to already-freed memory. + // Using _cachedBotGuid is safe - it was captured in the constructor. + if (_sharedBlackboard && !_cachedBotGuid.IsEmpty()) { - BlackboardManager::RemoveBotBlackboard(_bot->GetGUID()); + BlackboardManager::RemoveBotBlackboard(_cachedBotGuid); _sharedBlackboard = nullptr; }