From d4e75e9bd4624ef839781f82799809ebbc9da09d Mon Sep 17 00:00:00 2001 From: Karrz1 <127596890+Karrz1@users.noreply.github.com> Date: Tue, 27 Oct 2026 15:04:15 +0000 Subject: [PATCH] Core/Channels: Fixed a memory leak when loading a custom chat channel using AllowTwoSide.Interaction.Channel when both factions already had a channel with the same name (#32027) (cherry picked from TrinityCore 05d99fde25f350d39f3bbabf4b97beeabfe817ab) --- src/server/game/Chat/Channels/ChannelMgr.cpp | 21 +++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/src/server/game/Chat/Channels/ChannelMgr.cpp b/src/server/game/Chat/Channels/ChannelMgr.cpp index bbf61353aa..71f196851f 100644 --- a/src/server/game/Chat/Channels/ChannelMgr.cpp +++ b/src/server/game/Chat/Channels/ChannelMgr.cpp @@ -83,7 +83,7 @@ ChannelMgr::~ChannelMgr() if (!Utf8toWStr(dbName, channelName)) { TC_LOG_ERROR("server.loading", "Failed to load custom chat channel '{}' from database - invalid utf8 sequence? Deleted.", dbName); - toDelete.push_back({ dbName, team }); + toDelete.emplace_back(std::move(dbName), team); continue; } @@ -91,24 +91,31 @@ ChannelMgr::~ChannelMgr() if (!mgr) { TC_LOG_ERROR("server.loading", "Failed to load custom chat channel '{}' from database - invalid team {}. Deleted.", dbName, team); - toDelete.push_back({ dbName, team }); + toDelete.emplace_back(std::move(dbName), team); continue; } - Channel* channel = new Channel(mgr->CreateCustomChannelGuid(), dbName, team, dbBanned); + auto [itr, isNew] = mgr->_customChannels.try_emplace(std::move(channelName)); + if (!isNew) + { + // with CONFIG_ALLOW_TWO_SIDE_INTERACTION_CHANNEL both teams share the same manager, so channels with the same name from each team collide + TC_LOG_ERROR("server.loading", "Failed to load custom chat channel '{}' (team {}) from database - a channel with the same name is already loaded. Skipped.", dbName, team); + continue; + } + + Channel* channel = itr->second = new Channel(mgr->CreateCustomChannelGuid(), dbName, team, dbBanned); channel->SetAnnounce(dbAnnounce); channel->SetOwnership(dbOwnership); channel->SetPassword(dbPass); - mgr->_customChannels.emplace(channelName, channel); ++count; } while (result->NextRow()); - for (std::pair const& pair : toDelete) + for (auto const& [dbName, team] : toDelete) { CharacterDatabasePreparedStatement* stmt = CharacterDatabase.GetPreparedStatement(CHAR_DEL_CHANNEL); - stmt->setString(0, pair.first); - stmt->setUInt32(1, pair.second); + stmt->setString(0, dbName); + stmt->setUInt32(1, team); CharacterDatabase.Execute(stmt); }