From cfe37d586d2513e0bfe9e4754a6c08c86773fb0e Mon Sep 17 00:00:00 2001 From: Yuqian Yang Date: Mon, 24 Nov 2025 14:34:11 +0800 Subject: Fix delete later in delete later destructor. --- src/platform/gui/UiApplication.cpp | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) (limited to 'src/platform/gui/UiApplication.cpp') diff --git a/src/platform/gui/UiApplication.cpp b/src/platform/gui/UiApplication.cpp index 56570125..de92e61a 100644 --- a/src/platform/gui/UiApplication.cpp +++ b/src/platform/gui/UiApplication.cpp @@ -8,14 +8,19 @@ namespace cru::platform::gui { void DeleteLaterPool::Add(Object* object) { pool_.push_back(object); } void DeleteLaterPool::Clean() { - std::unordered_set deleted; - for (auto object : pool_) { - if (!deleted.contains(object)) { - deleted.insert(object); - delete object; + // Destructors of objects might add more objects to delete later. So the safe + // implementation is to copy current pool to avoid modification during + // iteration. + while (!pool_.empty()) { + std::vector copy = std::move(pool_); + std::unordered_set deleted; + for (auto object : copy) { + if (!deleted.contains(object)) { + deleted.insert(object); + delete object; + } } } - pool_.clear(); } namespace { -- cgit v1.2.3