From 07df20755514c99a373c3ab2617fa3c0f52806a8 Mon Sep 17 00:00:00 2001 From: Nathan Young Date: Sun, 8 Nov 2026 23:20:41 -0500 Subject: [PATCH] Name what still holds a listed AppKit window The settle loop ran its full half second and the count stayed at one, so the window is not merely waiting on AppKit's bookkeeping; something is holding it. Rather than guess a third time, the observer now names every window still listed, with its title, content view class, visibility, releasedWhenClosed flag and retain count. That distinguishes the toolkit keeping a closed window alive from an owner that never let go, and the fixture is compiled without ARC so the count is readable. Context::clear() also loses a condition I added without evidence. It required pthread_main_np() as well as a matching owning thread before disposing, where the other providers dispose on a matching owner alone. A context can only be used on the main thread, so a matching owner is already the main thread and the extra test could only defer a disposal the others perform at once -- which is exactly the shape of this failure. If that was the cause this fixes it, and if not the diagnostics say what is. Co-Authored-By: Claude Opus 5 --- nilcompiler/src/ui_runtime_appkit.mm | 5 ++++- tests/ui_registry_observer.hpp | 18 +++++++++++++++--- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/nilcompiler/src/ui_runtime_appkit.mm b/nilcompiler/src/ui_runtime_appkit.mm index 0ebb533..58c096a 100644 --- a/nilcompiler/src/ui_runtime_appkit.mm +++ b/nilcompiler/src/ui_runtime_appkit.mm @@ -468,7 +468,10 @@ void dispose() noexcept { if (!impl_) return; auto old = std::move(impl_); old->active.store(false); - if (old->owner == std::this_thread::get_id() && pthread_main_np()) old->dispose(); + // A context can only be used on the main thread, so an owning thread that + // matches is already the main thread; testing for it again only risked + // deferring a disposal the other providers perform immediately. + if (old->owner == std::this_thread::get_id()) old->dispose(); else if (!old->nodes.empty()) { try { std::lock_guard lock(Impl::deferred_mutex()); Impl::deferred().push_back(std::move(old)); } catch (...) { /* Callbacks hold no owning reference; a failed defer may leak windows, never free twice. */ } diff --git a/tests/ui_registry_observer.hpp b/tests/ui_registry_observer.hpp index b1ba7d8..a26b823 100644 --- a/tests/ui_registry_observer.hpp +++ b/tests/ui_registry_observer.hpp @@ -113,9 +113,21 @@ class UiRegistryObserver { // than only that the numbers differed. std::string detail() const { #if defined(__APPLE__) - unsigned shown = 0; - for (NSWindow *window in NSApp.windows) if (window.visible) ++shown; - return " (listed " + std::to_string(listed()) + ", of those visible " + std::to_string(shown) + ")"; + // Name every window still listed, with how many references are holding + // it, so a lingering one says whether the toolkit or an owner is keeping + // it alive. The fixture is compiled without ARC, so the count is readable. + std::string report = " (listed " + std::to_string(listed()) + ")"; + for (NSWindow *window in NSApp.windows) { + const char *title = window.title.length ? window.title.UTF8String : ""; + const char *content = window.contentView + ? NSStringFromClass([window.contentView class]).UTF8String : ""; + report += std::string("\n still listed: \"") + (title ? title : "?") + "\" content=" + + (content ? content : "?") + + " visible=" + (window.visible ? "1" : "0") + + " released_when_closed=" + (window.releasedWhenClosed ? "1" : "0") + + " refs=" + std::to_string(CFGetRetainCount((__bridge CFTypeRef)window)); + } + return report; #else return " (observed " + std::to_string(count()) + ")"; #endif