From 61dea382c968a7ebb7a03a6eb8076b33cf986d05 Mon Sep 17 00:00:00 2001 From: Mikusch <25514044+Mikusch@users.noreply.github.com> Date: Mon, 24 Aug 2026 21:40:41 +0200 Subject: [PATCH] Fix crash when deleting a Menu handle during callbacks (#2536) * Root menu Handles during callbacks * Delete unused AutoHandleRooter struct * Fix crash when a vote is cancelled from its start callback * menus: root vote menu during start callback --------- Co-authored-by: Nicholas Hastings --- core/MenuStyle_Base.cpp | 5 +++++ core/MenuVoting.cpp | 13 ++++++++++++- core/logic/HandleSys.h | 23 ----------------------- 3 files changed, 17 insertions(+), 24 deletions(-) diff --git a/core/MenuStyle_Base.cpp b/core/MenuStyle_Base.cpp index 491bbe69..30363477 100644 --- a/core/MenuStyle_Base.cpp +++ b/core/MenuStyle_Base.cpp @@ -97,6 +97,9 @@ void BaseMenuStyle::_CancelClientMenu(int client, MenuCancelReason reason, bool RemoveClientFromWatch(client); } + Handle_t hndl = menu ? menu->GetHandle() : BAD_HANDLE; + AutoHandleRooter ahr(hndl); + /* Fire callbacks */ mh->OnMenuCancel(menu, client, reason); @@ -576,6 +579,8 @@ bool BaseMenuStyle::RedoClientMenu(int client, ItemOrder order) CBaseMenuPlayer *player = GetMenuPlayer(client); menu_states_t &states = player->states; + AutoHandleRooter ahr(states.menu ? states.menu->GetHandle() : BAD_HANDLE); + player->bAutoIgnore = true; IMenuPanel *display = g_Menus.RenderMenu(client, states, order); if (!display) diff --git a/core/MenuVoting.cpp b/core/MenuVoting.cpp index de38a24e..a70c0a64 100644 --- a/core/MenuVoting.cpp +++ b/core/MenuVoting.cpp @@ -41,6 +41,7 @@ #include #include #include "logic_bridge.h" +#include "AutoHandleRooter.h" float g_next_vote = 0.0f; @@ -363,7 +364,14 @@ void VoteMenuHandler::StartVoting() m_bStarted = true; - m_pHandler->OnMenuVoteStart(m_pCurMenu); + IBaseMenu *menu = m_pCurMenu; + AutoHandleRooter ahr(menu->GetHandle()); + m_pHandler->OnMenuVoteStart(menu); + + if (!m_bStarted) + { + return; + } m_displayTimer = g_Timers.CreateTimer(this, 1.0, NULL, TIMER_FLAG_REPEAT|TIMER_FLAG_NO_MAPCHANGE); @@ -426,6 +434,7 @@ void VoteMenuHandler::EndVoting() IBaseMenu *menu = m_pCurMenu; IMenuHandler *handler = m_pHandler; InternalReset(); + AutoHandleRooter ahr(menu ? menu->GetHandle() : BAD_HANDLE); handler->OnMenuVoteCancel(menu, VoteCancel_Generic); handler->OnMenuEnd(menu, MenuEnd_VotingCancelled); return; @@ -455,6 +464,7 @@ void VoteMenuHandler::EndVoting() IBaseMenu *menu = m_pCurMenu; IMenuHandler *handler = m_pHandler; InternalReset(); + AutoHandleRooter ahr(menu ? menu->GetHandle() : BAD_HANDLE); handler->OnMenuVoteCancel(menu, VoteCancel_NoVotes); handler->OnMenuEnd(menu, MenuEnd_VotingCancelled); return; @@ -486,6 +496,7 @@ void VoteMenuHandler::EndVoting() InternalReset(); /* Send vote info */ + AutoHandleRooter ahr(menu ? menu->GetHandle() : BAD_HANDLE); handler->OnMenuVoteResults(menu, &vote); handler->OnMenuEnd(menu, MenuEnd_VotingDone); } diff --git a/core/logic/HandleSys.h b/core/logic/HandleSys.h index 2fc786b8..dee2f6e4 100644 --- a/core/logic/HandleSys.h +++ b/core/logic/HandleSys.h @@ -243,27 +243,4 @@ private: extern HandleSystem g_HandleSys; -struct AutoHandleRooter -{ -public: - AutoHandleRooter(Handle_t hndl) - { - if (hndl != BAD_HANDLE) - this->hndl = g_HandleSys.FastCloneHandle(hndl); - else - this->hndl = BAD_HANDLE; - } - - ~AutoHandleRooter() - { - if (hndl != BAD_HANDLE) - { - HandleSecurity sec(g_pCoreIdent, g_pCoreIdent); - g_HandleSys.FreeHandle(hndl, &sec); - } - } -private: - Handle_t hndl; -}; - #endif //_INCLUDE_SOURCEMOD_HANDLESYSTEM_H_