From 8ee93bc3270d6401fd44d837e694b0ec461c90cc Mon Sep 17 00:00:00 2001 From: luis Date: Sat, 14 Nov 2026 20:58:22 -0300 Subject: [PATCH] mem leaks fix --- .../game/CraftingOrders/CraftingOrderMgr.cpp | 5 ++++ .../game/CraftingOrders/CraftingOrderMgr.h | 2 ++ .../game/Handlers/BattleGroundHandler.cpp | 17 +++++++++++ src/server/game/Housing/HousingMgr.cpp | 6 ++++ src/server/game/Housing/HousingMgr.h | 2 ++ src/server/game/Scripting/ScriptHelpers.cpp | 6 ++++ src/server/game/Scripting/ScriptHelpers.h | 4 +++ src/server/game/Server/WorldSession.cpp | 11 +++++++ .../game/WeeklyRewards/WeeklyRewardsMgr.cpp | 5 ++++ .../game/WeeklyRewards/WeeklyRewardsMgr.h | 2 ++ .../Orderhalls/orderhall_legion.cpp | 4 ++- .../Followship_bots_battleground_handler.cpp | 30 +++++++++++++++++-- src/server/scripts/Spells/spell_dk.cpp | 28 +++++++++++++++++ 13 files changed, 118 insertions(+), 4 deletions(-) diff --git a/src/server/game/CraftingOrders/CraftingOrderMgr.cpp b/src/server/game/CraftingOrders/CraftingOrderMgr.cpp index 057be9ea..5eeffb41 100644 --- a/src/server/game/CraftingOrders/CraftingOrderMgr.cpp +++ b/src/server/game/CraftingOrders/CraftingOrderMgr.cpp @@ -418,6 +418,11 @@ bool CraftingOrderMgr::IsIgnoring(ObjectGuid owner, ObjectGuid other) const return std::find(it->second.begin(), it->second.end(), other) != it->second.end(); } +void CraftingOrderMgr::ClearIgnoreList(ObjectGuid owner) +{ + _ignoreLists.erase(owner); +} + bool CraftingOrderMgr::ReportPlayer(uint64 orderId, ObjectGuid reporter, ObjectGuid reported, int32 reportType, int32 majorCategory, int32 minorCategoryFlags, std::string const& comment) const { diff --git a/src/server/game/CraftingOrders/CraftingOrderMgr.h b/src/server/game/CraftingOrders/CraftingOrderMgr.h index f6e9039b..642d96d0 100644 --- a/src/server/game/CraftingOrders/CraftingOrderMgr.h +++ b/src/server/game/CraftingOrders/CraftingOrderMgr.h @@ -118,6 +118,8 @@ public: // needed (it is re-synced every session). Used to reject personal orders aimed at an ignored crafter. void SetIgnoreList(ObjectGuid owner, std::vector ignored); bool IsIgnoring(ObjectGuid owner, ObjectGuid other) const; + // Drops the session-only ignore list of a player that is leaving the world. + void ClearIgnoreList(ObjectGuid owner); // Records a player report filed against a participant of a crafting order (CMSG_CRAFTING_ORDER_REPORT_PLAYER). // The report is persisted to `crafting_order_reports` (character DB) for GM review; there is no client response. diff --git a/src/server/game/Handlers/BattleGroundHandler.cpp b/src/server/game/Handlers/BattleGroundHandler.cpp index 3acf2e1e..7d0d2e35 100644 --- a/src/server/game/Handlers/BattleGroundHandler.cpp +++ b/src/server/game/Handlers/BattleGroundHandler.cpp @@ -1463,8 +1463,14 @@ namespace uint32 BattlemasterListID = 0; uint16 Bracket = 0; bool TournamentRules = false; + time_t CreatedTime = 0; }; + // The client's war-game prompt times out on its own, so a challenge that was + // ignored (or whose opponent logged off) will never be answered. Expire it + // instead of keeping one entry per opponent leader for the whole uptime. + constexpr uint32 WAR_GAME_REQUEST_TIMEOUT = 2 * MINUTE; + std::unordered_map g_wargameRequests; } @@ -1472,6 +1478,16 @@ void WorldSession::HandleStartWarGame(WorldPackets::Battleground::StartWarGame& { Player* initiator = _player; + // Drop challenges nobody answered; the prompt is long gone by now. + time_t const now = GameTime::GetGameTime(); + for (auto itr = g_wargameRequests.begin(); itr != g_wargameRequests.end();) + { + if (now >= time_t(itr->second.CreatedTime) + WAR_GAME_REQUEST_TIMEOUT) + itr = g_wargameRequests.erase(itr); + else + ++itr; + } + // Only a party leader may issue a war-game challenge on behalf of their group. Group* group = initiator->GetGroup(); if (!group || group->GetLeaderGUID() != initiator->GetGUID()) @@ -1501,6 +1517,7 @@ void WorldSession::HandleStartWarGame(WorldPackets::Battleground::StartWarGame& req.BattlemasterListID = packet.BattlemasterListID; req.Bracket = packet.Bracket; req.TournamentRules = packet.TournamentRules; + req.CreatedTime = now; // Prompt the opposing leader to accept or decline. WorldPackets::Battleground::CheckWargameEntry check; diff --git a/src/server/game/Housing/HousingMgr.cpp b/src/server/game/Housing/HousingMgr.cpp index 0bb6b5bf..55a24f6b 100644 --- a/src/server/game/Housing/HousingMgr.cpp +++ b/src/server/game/Housing/HousingMgr.cpp @@ -677,6 +677,12 @@ Optional HousingMgr::TakePendingPlotTeleport(Ob return pending; } +void HousingMgr::RemovePendingPlotTeleport(ObjectGuid playerGuid) +{ + std::lock_guard lock(_pendingPlotTeleportsLock); + _pendingPlotTeleports.erase(playerGuid); +} + Position HousingMgr::GetDefaultHousePosition(NeighborhoodPlotData const& plot) const { // A never-moved house root sits at local (0,0,0) under the plot room (the GO row turned half a revolution). diff --git a/src/server/game/Housing/HousingMgr.h b/src/server/game/Housing/HousingMgr.h index 0a332b0e..c9474c93 100644 --- a/src/server/game/Housing/HousingMgr.h +++ b/src/server/game/Housing/HousingMgr.h @@ -275,6 +275,8 @@ public: }; void SetPendingPlotTeleport(ObjectGuid playerGuid, WorldLocation const& dest, uint32 neighborhoodId); Optional TakePendingPlotTeleport(ObjectGuid playerGuid); + // Drops a teleport that was never consumed (the caster left the world first). + void RemovePendingPlotTeleport(ObjectGuid playerGuid); // Find a plot by its cornerstone GO entry within a specific neighborhood map NeighborhoodPlotData const* GetPlotByCornerstoneEntry(uint32 neighborhoodMapId, uint32 cornerstoneGoEntry) const; diff --git a/src/server/game/Scripting/ScriptHelpers.cpp b/src/server/game/Scripting/ScriptHelpers.cpp index e6e35895..f95e8039 100644 --- a/src/server/game/Scripting/ScriptHelpers.cpp +++ b/src/server/game/Scripting/ScriptHelpers.cpp @@ -88,6 +88,12 @@ namespace ScriptHelpers { return player && s_forceClearedPlayers.erase(player->GetGUID().GetCounter()) > 0; } + + void EraseFakePartyCleared(Player* player) + { + if (player) + s_forceClearedPlayers.erase(player->GetGUID().GetCounter()); + } //WowCommunity void SendClearFakeParty(Player* player) diff --git a/src/server/game/Scripting/ScriptHelpers.h b/src/server/game/Scripting/ScriptHelpers.h index b3f9826e..540ef562 100644 --- a/src/server/game/Scripting/ScriptHelpers.h +++ b/src/server/game/Scripting/ScriptHelpers.h @@ -68,6 +68,10 @@ namespace ScriptHelpers // when the bot composition hash is unchanged. TC_GAME_API bool ConsumeFakePartyCleared(Player* player); + // Same bookkeeping, without reporting a hit: used when a player leaves the + // world, so a clear nobody consumed does not survive the session. + TC_GAME_API void EraseFakePartyCleared(Player* player); + // Wrapper function to send a party member full state packet for a bot to a player // This constructs the packet internally in the core to avoid linking issues with dynamic loading // Takes raw bot data instead of PartyMemberStats to avoid needing PartyPackets.h in scripts diff --git a/src/server/game/Server/WorldSession.cpp b/src/server/game/Server/WorldSession.cpp index 615c6093..d37da391 100644 --- a/src/server/game/Server/WorldSession.cpp +++ b/src/server/game/Server/WorldSession.cpp @@ -31,6 +31,7 @@ #include "ChatPackets.h" #include "ClientConfigPackets.h" #include "Containers.h" +#include "CraftingOrderMgr.h" #include "DatabaseEnv.h" #include "DB2Stores.h" #include "GameTime.h" @@ -53,8 +54,10 @@ #include "Random.h" #include "RBAC.h" #include "RealmList.h" +#include "ScriptHelpers.h" #include "ScriptMgr.h" #include "SocialMgr.h" +#include "WeeklyRewardsMgr.h" #include "World.h" #include "WorldSocket.h" #include @@ -582,6 +585,14 @@ void WorldSession::LogoutPlayer(bool save) sLFGListMgr.UnregisterSearch(_player->GetGUID()); // The house finder ignore list is DB backed, drop the cached copy with the session. sHousingMgr.RemoveIgnoredNeighborhoods(_player->GetGUID()); + // A pending housing teleport that was never taken dies with the session. + sHousingMgr.RemovePendingPlotTeleport(_player->GetGUID()); + // Crafting-order ignore lists are session-only; drop them with the session. + sCraftingOrderMgr.ClearIgnoreList(_player->GetGUID()); + // Bot-roster clears nobody consumed must not outlive the session. + ScriptHelpers::EraseFakePartyCleared(_player); + // The Great Vault cache is written through on every change, so drop it here. + sWeeklyRewardsMgr.UnloadCharacter(_player->GetGUID()); //WowCommunity if (!_player->GetLootGUID().IsEmpty()) DoLootReleaseAll(); diff --git a/src/server/game/WeeklyRewards/WeeklyRewardsMgr.cpp b/src/server/game/WeeklyRewards/WeeklyRewardsMgr.cpp index 948d881f..8ce2fa44 100644 --- a/src/server/game/WeeklyRewards/WeeklyRewardsMgr.cpp +++ b/src/server/game/WeeklyRewards/WeeklyRewardsMgr.cpp @@ -246,6 +246,11 @@ void WeeklyRewardsMgr::SaveVault(ObjectGuid guid) CharacterDatabase.CommitTransaction(trans); } +void WeeklyRewardsMgr::UnloadCharacter(ObjectGuid guid) +{ + _vaults.erase(guid); +} + void WeeklyRewardsMgr::RecordActivity(Player* player, WeeklyRewards::ActivityType type, uint32 level) { if (!player || type >= WeeklyRewards::ActivityType::Max) diff --git a/src/server/game/WeeklyRewards/WeeklyRewardsMgr.h b/src/server/game/WeeklyRewards/WeeklyRewardsMgr.h index c04b77a6..da74649c 100644 --- a/src/server/game/WeeklyRewards/WeeklyRewardsMgr.h +++ b/src/server/game/WeeklyRewards/WeeklyRewardsMgr.h @@ -128,6 +128,8 @@ public: void LoadCharacter(ObjectGuid guid); // lazy load on demand void SaveVault(ObjectGuid guid); + // Drop the cached vault of a character that is leaving the world (it is saved on every mutation). + void UnloadCharacter(ObjectGuid guid); // --- Great Vault slots --- diff --git a/src/server/scripts/BrokenIsles/Orderhalls/orderhall_legion.cpp b/src/server/scripts/BrokenIsles/Orderhalls/orderhall_legion.cpp index 4d98d198..0146f001 100644 --- a/src/server/scripts/BrokenIsles/Orderhalls/orderhall_legion.cpp +++ b/src/server/scripts/BrokenIsles/Orderhalls/orderhall_legion.cpp @@ -174,7 +174,9 @@ public: void OnLogout(Player* player) override { - _messengers.erase(player->GetGUID()); // the personal summon despawns with the player / on its own timer + // the personal summon despawns with the player / on its own timer + std::lock_guard guard(_messengersLock); + _messengers.erase(player->GetGUID()); } private: diff --git a/src/server/scripts/Custom/FollowshipBots/Handlers/Followship_bots_battleground_handler.cpp b/src/server/scripts/Custom/FollowshipBots/Handlers/Followship_bots_battleground_handler.cpp index 5cadd447..62042001 100644 --- a/src/server/scripts/Custom/FollowshipBots/Handlers/Followship_bots_battleground_handler.cpp +++ b/src/server/scripts/Custom/FollowshipBots/Handlers/Followship_bots_battleground_handler.cpp @@ -37,6 +37,7 @@ #include "Creature.h" #include "Log.h" #include "Map.h" +#include "MapManager.h" #include "ObjectAccessor.h" #include "ObjectMgr.h" #include "Player.h" @@ -534,11 +535,34 @@ namespace FSBBattleground } } - std::unordered_map> SpawnedBotGuids; + // Spawned bot guids are only needed while their battleground map lives, but + // nothing tells this module when the map goes away. Store the map identity + // with the list and drop entries whose map no longer exists whenever a bot + // is spawned, so the store cannot grow across battlegrounds (a recycled + // BattlegroundMap* must not inherit the previous instance's guids either). + struct SpawnedBotGuidsEntry + { + uint32 MapId = 0; + uint32 InstanceId = 0; + std::vector Guids; + }; + + std::unordered_map SpawnedBotGuids; void AddBotSpawnGuid(BattlegroundMap* battlegroundMap, ObjectGuid guid) { - SpawnedBotGuids[battlegroundMap].push_back(guid); + for (auto itr = SpawnedBotGuids.begin(); itr != SpawnedBotGuids.end();) + { + if (sMapMgr->FindMap(itr->second.MapId, itr->second.InstanceId) != itr->first) + itr = SpawnedBotGuids.erase(itr); + else + ++itr; + } + + SpawnedBotGuidsEntry& entry = SpawnedBotGuids[battlegroundMap]; + entry.MapId = battlegroundMap->GetId(); + entry.InstanceId = battlegroundMap->GetInstanceId(); + entry.Guids.push_back(guid); } void ClearSpawnedBotGuids(BattlegroundMap* battlegroundMap) @@ -658,7 +682,7 @@ namespace FSBBattleground { static std::vector const empty; auto it = SpawnedBotGuids.find(battlegroundMap); - return it != SpawnedBotGuids.end() ? it->second : empty; + return it != SpawnedBotGuids.end() ? it->second.Guids : empty; } Unit* FindFriendlyAssistTarget(Creature* bot, float range) diff --git a/src/server/scripts/Spells/spell_dk.cpp b/src/server/scripts/Spells/spell_dk.cpp index 5b8d482b..12510836 100644 --- a/src/server/scripts/Spells/spell_dk.cpp +++ b/src/server/scripts/Spells/spell_dk.cpp @@ -2621,6 +2621,11 @@ struct FesteringStrikeBlight return true; } + static void Clear(ObjectGuid casterGuid) + { + Charges().erase(casterGuid); + } + private: static std::unordered_map& Charges() { @@ -2629,6 +2634,28 @@ private: } }; +// The blight is only consumed by Scourge Strike, so a caster that logs out with +// charges left would otherwise keep its GUID in the map for the whole uptime. +class player_dk_festering_strike_cleanup : public PlayerScript +{ +public: + player_dk_festering_strike_cleanup() : PlayerScript("player_dk_festering_strike_cleanup") { } + + void OnLogout(Player* player) override + { + // Only a Death Knight can apply Festering Strike's blight. + if (player->GetClass() != CLASS_DEATH_KNIGHT) + return; + + FesteringStrikeBlight::Clear(player->GetGUID()); + } + + void OnDelete(ObjectGuid guid, uint32 /*accountId*/) override + { + FesteringStrikeBlight::Clear(guid); + } +}; + class spell_dk_festering_strike : public SpellScript { bool Validate(SpellInfo const* /*spellInfo*/) override @@ -3414,6 +3441,7 @@ void AddSC_deathknight_spell_scripts() new player_dk_frostreaper_cleanup(); new player_dk_on_a_paler_horse(); + new player_dk_festering_strike_cleanup(); RegisterAreaTriggerAI(at_dk_death_and_decay); }