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