aboutsummaryrefslogtreecommitdiff
path: root/src/ui/controls/Control.cpp
diff options
context:
space:
mode:
authorYuqian Yang <crupest@crupest.life>2025-11-17 13:52:52 +0800
committerYuqian Yang <crupest@crupest.life>2025-11-17 13:52:52 +0800
commitf7c4d19df66c602d74795e98ce2ee4390d06fbb4 (patch)
tree5d91dc18ae8129652d4c5b88234d0bb7b93dcf13 /src/ui/controls/Control.cpp
parentc9f31440d65a867e2316e9a19ee3f5db2d0e53a7 (diff)
downloadcru-f7c4d19df66c602d74795e98ce2ee4390d06fbb4.tar.gz
cru-f7c4d19df66c602d74795e98ce2ee4390d06fbb4.tar.bz2
cru-f7c4d19df66c602d74795e98ce2ee4390d06fbb4.zip
Harden delete after free.
Diffstat (limited to 'src/ui/controls/Control.cpp')
-rw-r--r--src/ui/controls/Control.cpp17
1 files changed, 15 insertions, 2 deletions
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);