From 88c47bcc4e2f7d351bd72ec8c32f314e508dd62e Mon Sep 17 00:00:00 2001 From: Mr-Wiseguy Date: Fri, 2 May 2025 06:33:44 -0400 Subject: [PATCH] Defer setting an element's text if it has children to fix race condition crash, bump version to 1.2.0-rc3 --- src/main/main.cpp | 2 +- src/ui/core/ui_context.cpp | 41 +++++++++++++++++++++++++++++++++- src/ui/core/ui_context.h | 1 + src/ui/elements/ui_element.cpp | 13 +++++++++-- 4 files changed, 53 insertions(+), 4 deletions(-) diff --git a/src/main/main.cpp b/src/main/main.cpp index a7981a9..3c2f321 100644 --- a/src/main/main.cpp +++ b/src/main/main.cpp @@ -48,7 +48,7 @@ #include "../../lib/rt64/src/contrib/stb/stb_image.h" -const std::string version_string = "1.2.0-rc2"; +const std::string version_string = "1.2.0-rc3"; template void exit_error(const char* str, Ts ...args) { diff --git a/src/ui/core/ui_context.cpp b/src/ui/core/ui_context.cpp index 2fbed61..40e9c8a 100644 --- a/src/ui/core/ui_context.cpp +++ b/src/ui/core/ui_context.cpp @@ -36,7 +36,8 @@ namespace recompui { Element root_element; Element* autofocus_element = nullptr; std::vector loose_elements; - std::unordered_set to_update; + std::unordered_set to_update; + std::vector> to_set_text; bool captures_input = true; bool captures_mouse = true; Context(Rml::ElementDocument* document) : document(document), root_element(document) {} @@ -67,6 +68,8 @@ enum class ContextErrorType { AddResourceToWrongContext, UpdateElementWithoutContext, UpdateElementInWrongContext, + SetTextElementWithoutContext, + SetTextElementInWrongContext, GetResourceWithoutOpen, GetResourceFailed, DestroyResourceWithoutOpen, @@ -119,6 +122,12 @@ void context_error(recompui::ContextId id, ContextErrorType type) { case ContextErrorType::UpdateElementInWrongContext: error_message = "Attempted to update a UI element in a different UI context than the one that's open"; break; + case ContextErrorType::SetTextElementWithoutContext: + error_message = "Attempted to set the text of a UI element with no open UI context"; + break; + case ContextErrorType::SetTextElementInWrongContext: + error_message = "Attempted to set the text of a UI element in a different UI context than the one that's open"; + break; case ContextErrorType::GetResourceWithoutOpen: error_message = "Attempted to get a UI resource with no open UI context"; break; @@ -407,6 +416,22 @@ void recompui::ContextId::process_updates() { static_cast(cur_resource->get())->handle_event(update_event); } + + std::vector> to_set_text = std::move(opened_context->to_set_text); + + // Delete the Rml elements that are pending deletion. + for (auto cur_text_update : to_set_text) { + resource_slotmap::key cur_key{ cur_text_update.first.slot_id }; + std::unique_ptr