fix: Ensure safe ID management in WidgetIdScope during ImGui frame initialization

This commit is contained in:
scawful
2025-10-02 11:01:30 -04:00
parent b77bd201e2
commit 3c9669d062
2 changed files with 59 additions and 24 deletions

View File

@@ -8,6 +8,7 @@
#include "absl/strings/str_format.h"
#include "absl/strings/str_join.h"
#include "absl/strings/str_split.h"
#include "imgui/imgui_internal.h" // For ImGuiContext internals
namespace yaze {
namespace gui {
@@ -16,13 +17,19 @@ namespace gui {
thread_local std::vector<std::string> WidgetIdScope::id_stack_;
WidgetIdScope::WidgetIdScope(const std::string& name) : name_(name) {
ImGui::PushID(name.c_str());
id_stack_.push_back(name);
// Only push ID if we're in an active ImGui frame with a valid window
// This prevents crashes during editor initialization before ImGui begins its frame
ImGuiContext* ctx = ImGui::GetCurrentContext();
if (ctx && ctx->CurrentWindow && !ctx->Windows.empty()) {
ImGui::PushID(name.c_str());
id_stack_.push_back(name);
}
}
WidgetIdScope::~WidgetIdScope() {
ImGui::PopID();
if (!id_stack_.empty()) {
// Only pop if we successfully pushed
if (!id_stack_.empty() && id_stack_.back() == name_) {
ImGui::PopID();
id_stack_.pop_back();
}
}