From 69b9a0ca1436fa6c338a8f7dd636c6082f6e983e Mon Sep 17 00:00:00 2001 From: past-due <30942300+past-due@users.noreply.github.com> Date: Tue, 10 Oct 2023 13:04:29 -0400 Subject: [PATCH] chatscreen: Fix possible crash handling chat / cheat codes If effects of the cheat code modified overlay screens / widgets in certain ways --- src/screens/chatscreen.cpp | 63 +++++++++++++++++++++++++------------- 1 file changed, 42 insertions(+), 21 deletions(-) diff --git a/src/screens/chatscreen.cpp b/src/screens/chatscreen.cpp index 4c3cb4a0f3b..b03739dbb82 100644 --- a/src/screens/chatscreen.cpp +++ b/src/screens/chatscreen.cpp @@ -201,29 +201,38 @@ void WzInGameChatBoxForm::initialize(WzChatMode initialChatMode, const W_EDITBOX ASSERT_OR_RETURN(, strongParent != nullptr, "No parent?"); auto enteredText = widg.getString(); - const char* pStr = enteredText.toUtf8().c_str(); - auto message = InGameChatMessage(selectedPlayer, pStr); - attemptCheatCode(message.text); // parse the message + WzChatMode currChatMode = strongParent->chatMode; + inputLoseFocus(); - switch (strongParent->chatMode) - { - case WzChatMode::Glob: - parseChatMessageModifiers(message); - break; - case WzChatMode::Team: - message.toAllies = true; - break; - } + auto weakParent = std::weak_ptr(strongParent); + widgScheduleTask([enteredText, currChatMode, weakParent]() { - if (strlen(message.text)) - { - message.send(); - } + // must process the enteredText inside a scheduled task as side effects of attemptCheatCode may modify the overlay screens / widgets - inputLoseFocus(); + const char* pStr = enteredText.toUtf8().c_str(); + auto message = InGameChatMessage(selectedPlayer, pStr); + attemptCheatCode(message.text); // parse the message + + switch (currChatMode) + { + case WzChatMode::Glob: + parseChatMessageModifiers(message); + break; + case WzChatMode::Team: + message.toAllies = true; + break; + } - strongParent->endChatBoxEditing(); - strongParent->closeParentScreen(); + if (strlen(message.text)) + { + message.send(); + } + + auto parent = weakParent.lock(); + ASSERT_OR_RETURN(, parent != nullptr, "No parent?"); + parent->endChatBoxEditing(); + parent->closeParentScreen(); + }); }); chatBox->setOnEscapeHandler([](W_EDITBOX& widg) { @@ -231,13 +240,25 @@ void WzInGameChatBoxForm::initialize(WzChatMode initialChatMode, const W_EDITBOX ASSERT_OR_RETURN(, strongParent != nullptr, "No parent?"); inputLoseFocus(); - strongParent->closeParentScreen(); + + auto weakParent = std::weak_ptr(strongParent); + widgScheduleTask([weakParent]() { + auto parent = weakParent.lock(); + ASSERT_OR_RETURN(, parent != nullptr, "No parent?"); + parent->closeParentScreen(); + }); }); chatBox->setOnEditingStoppedHandler([](W_EDITBOX& widg) { auto strongParent = std::dynamic_pointer_cast(widg.parent()); ASSERT_OR_RETURN(, strongParent != nullptr, "No parent?"); - strongParent->endChatBoxEditing(); + + auto weakParent = std::weak_ptr(strongParent); + widgScheduleTask([weakParent]() { + auto parent = weakParent.lock(); + ASSERT_OR_RETURN(, parent != nullptr, "No parent?"); + parent->endChatBoxEditing(); + }); }); if (onTabHandler)