A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.
TypeScript 2d-game-engine3d3d-game-engineandroidcppecsgame-editorgame-enginegamedeviostypescriptwebassemblywebglwebgpuwechat-minigame
fix: Prevent crash when widgets are deleted during event handling
esengine2e201cd2fa7d
diff --git a/src/esengine/editor/panels/AssetBrowserPanel.cpp b/src/esengine/editor/panels/AssetBrowserPanel.cppindex 545386700..4440a9dc8 100644--- a/src/esengine/editor/panels/AssetBrowserPanel.cpp+++ b/src/esengine/editor/panels/AssetBrowserPanel.cpp@@ -93,6 +93,24 @@ void AssetBrowserPanel::onActivated() {}}+void AssetBrowserPanel::render(ui::UIBatchRenderer& renderer) {+ // Process deferred navigation to avoid deleting objects during their event handlers+ if (needsRefreshAssetList_) {+ needsRefreshAssetList_ = false;+ currentPath_ = pendingNavigatePath_;+ refreshAssetList();++ auto it = pathToNode_.find(pendingNavigatePath_);+ if (it != pathToNode_.end()) {+ folderTree_->selectNode(it->second);+ folderTree_->setNodeExpanded(it->second, true);+ }+ pendingNavigatePath_.clear();+ }++ DockPanel::render(renderer);+}+// =============================================================================// UI Building// =============================================================================@@ -279,9 +297,9 @@ void AssetBrowserPanel::refreshAssetList() {return;#endif+ itemConnections_.disconnectAll();currentAssets_.clear();assetGridPanel_->clearChildren();- itemConnections_.disconnectAll();if (!FileSystem::directoryExists(currentPath_)) {ES_LOG_WARN("AssetBrowserPanel::refreshAssetList: directory not found: {}", currentPath_);@@ -386,17 +404,15 @@ void AssetBrowserPanel::onAssetItemClicked(const std::string& path) {}void AssetBrowserPanel::onAssetItemDoubleClicked(const std::string& path) {- if (FileSystem::directoryExists(path)) {- currentPath_ = path;- refreshAssetList();+ // Copy path - the source object will be destroyed when we refresh+ std::string pathCopy = path;- auto it = pathToNode_.find(path);- if (it != pathToNode_.end()) {- folderTree_->selectNode(it->second);- folderTree_->setNodeExpanded(it->second, true);- }+ if (FileSystem::directoryExists(pathCopy)) {+ // Defer the refresh to avoid deleting the object that's currently handling the event+ pendingNavigatePath_ = pathCopy;+ needsRefreshAssetList_ = true;} else {- onAssetDoubleClicked.publish(path);+ onAssetDoubleClicked.publish(pathCopy);}}diff --git a/src/esengine/editor/panels/AssetBrowserPanel.hpp b/src/esengine/editor/panels/AssetBrowserPanel.hppindex 1a3c18b0e..af997299d 100644--- a/src/esengine/editor/panels/AssetBrowserPanel.hpp+++ b/src/esengine/editor/panels/AssetBrowserPanel.hpp@@ -58,6 +58,7 @@ public:Signal<void(const std::string&)> onAssetDoubleClicked;bool onMouseDown(const ui::MouseButtonEvent& event) override;+ void render(ui::UIBatchRenderer& renderer) override;protected:void onActivated() override;@@ -103,6 +104,8 @@ private:std::string selectedAssetPath_;bool needsRebuild_ = false;+ bool needsRefreshAssetList_ = false;+ std::string pendingNavigatePath_;AssetDatabase& assetDB_;ThumbnailGenerator& thumbnailGen_;diff --git a/src/esengine/ui/UIContext.cpp b/src/esengine/ui/UIContext.cppindex ff03cf5b6..58e9df16a 100644--- a/src/esengine/ui/UIContext.cpp+++ b/src/esengine/ui/UIContext.cpp@@ -475,6 +475,20 @@ void UIContext::setFocus(Widget* widget) {}}+void UIContext::clearWidgetReferences(Widget* widget) {+ if (!widget) return;++ if (focusedWidget_ == widget) {+ focusedWidget_ = nullptr;+ }+ if (hoveredWidget_ == widget) {+ hoveredWidget_ = nullptr;+ }+ if (pressedWidget_ == widget) {+ pressedWidget_ = nullptr;+ }+}+// =============================================================================// Internal// =============================================================================diff --git a/src/esengine/ui/UIContext.hpp b/src/esengine/ui/UIContext.hppindex 9776c89cf..8b0191225 100644--- a/src/esengine/ui/UIContext.hpp+++ b/src/esengine/ui/UIContext.hpp@@ -309,6 +309,12 @@ public:*/void clearFocus() { setFocus(nullptr); }+ /**+ * @brief Clears all references to a widget (called when widget is destroyed)+ * @param widget Widget being destroyed+ */+ void clearWidgetReferences(Widget* widget);+// =========================================================================// Overlay Management// =========================================================================diff --git a/src/esengine/ui/widgets/Widget.cpp b/src/esengine/ui/widgets/Widget.cppindex 6a9dfd910..c5c316ae7 100644--- a/src/esengine/ui/widgets/Widget.cpp+++ b/src/esengine/ui/widgets/Widget.cpp@@ -12,6 +12,7 @@#include "Widget.hpp"#include "../layout/Layout.hpp"#include "../rendering/UIBatchRenderer.hpp"+#include "../UIContext.hpp"namespace esengine::ui {@@ -248,6 +249,9 @@ bool Widget::containsPoint(f32 x, f32 y) const {// =============================================================================void Widget::setContext(UIContext* context) {+ if (context_ && context_ != context) {+ context_->clearWidgetReferences(this);+ }context_ = context;for (auto& child : children_) {child->setContext(context);