From f7c4d19df66c602d74795e98ce2ee4390d06fbb4 Mon Sep 17 00:00:00 2001 From: Yuqian Yang Date: Mon, 17 Nov 2025 13:52:52 +0800 Subject: Harden delete after free. --- src/ui/controls/Control.cpp | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) (limited to 'src/ui/controls/Control.cpp') diff --git a/src/ui/controls/Control.cpp b/src/ui/controls/Control.cpp index e903b5b3..9c0fc537 100644 --- a/src/ui/controls/Control.cpp +++ b/src/ui/controls/Control.cpp @@ -1,4 +1,5 @@ #include "cru/ui/controls/Control.h" +#include "cru/base/log/Logger.h" #include "cru/ui/controls/Window.h" #include "cru/platform/gui/Cursor.h" @@ -27,11 +28,14 @@ Control::Control() { Control::~Control() { if (auto window = GetWindow()) { if (window->IsInEventHandling()) { - // Don't delete control during event handling. Use DeleteLater. - std::terminate(); + CRU_LOG_TAG_WARN( + "Better use delete later to delete control during event handling."); } } + if (auto window = GetWindow()) { + window->NotifyControlDestroyed(this); + } RemoveFromParent(); } @@ -58,6 +62,15 @@ void Control::SetParent(Control* parent) { OnParentChanged(old_parent, parent); } +bool Control::HasAncestor(Control* control) { + auto parent = this; + while (parent) { + if (parent == control) return true; + parent = parent->GetParent(); + } + return false; +} + void Control::RemoveFromParent() { if (parent_) { parent_->RemoveChild(this); -- cgit v1.2.3