From d222a9f99be21b2fc7c86c8551acf0bd179259b5 Mon Sep 17 00:00:00 2001 From: sogladev Date: Wed, 18 Mar 2026 03:47:28 +0100 Subject: [PATCH] fix(Core/Unit): avoid iterator invalidation in RemoveAllControlled (#25108) Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --- src/server/game/Entities/Unit/Unit.cpp | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/src/server/game/Entities/Unit/Unit.cpp b/src/server/game/Entities/Unit/Unit.cpp index 721361e57..ab71b9ac8 100644 --- a/src/server/game/Entities/Unit/Unit.cpp +++ b/src/server/game/Entities/Unit/Unit.cpp @@ -8080,10 +8080,11 @@ void Unit::RemoveAllControlled(bool onDeath /*= false*/) for (auto it = m_Controlled.begin(); it != m_Controlled.end();) { Unit* target = *it; + ++it; if (target->GetCharmerGUID() == GetGUID()) { - it = m_Controlled.erase(it); + m_Controlled.erase(target); target->RemoveCharmAuras(); } else if (target->GetOwnerGUID() == GetGUID() && target->IsSummon()) @@ -8092,23 +8093,15 @@ void Unit::RemoveAllControlled(bool onDeath /*= false*/) if (onDeath) if (TempSummon* ts = target->ToTempSummon()) if (ts->m_Properties && ts->m_Properties->Type == SUMMON_TYPE_LIGHTWELL) - { - ++it; continue; - } if (!(onDeath && !IsPlayer() && target->IsGuardian())) - { target->ToTempSummon()->UnSummon(); - it = m_Controlled.erase(it); - } - else - ++it; } else { LOG_ERROR("entities.unit", "Unit {} is trying to release unit {} which is neither charmed nor owned by it", GetEntry(), target->GetEntry()); - ++it; + m_Controlled.erase(target); } } }