From 8d4b1e0a28edbf57f308547ca0b5a64a206d9a2a Mon Sep 17 00:00:00 2001 From: Nathan Young Date: Sun, 8 Nov 2026 23:37:31 -0500 Subject: [PATCH] Count provider-owned windows on AppKit, not listed ones The diagnostics settled it: "UI lifecycle VM window" content=NyUiContainer visible=0 released_when_closed=0 refs=24. The window was closed and the provider had let go; AppKit was holding it, through twenty-four references, and no amount of settling was going to change that. So the metric was wrong, not the provider. Two AppKit facts break the GTK-shaped question. A closed window stays in the application's window list until it deallocates, which AppKit defers indefinitely, and the lifecycle fixture never orders its window in, so isVisible cannot separate a live window from a released one either -- both read false. The provider does leave a usable marker: it installs a delegate when it creates a window and clears it on every release path -- close, dispose and the creation rollback. Counting its own content views that still have a delegate asks exactly what the fixture means, has the provider let this window go, and stays independent of the provider's handle map, which is what a stale handle could otherwise hide. The assertions and their count are unchanged, so the fixture still reports the 235 checks the display contract requires. My two earlier attempts at this failure -- a run-loop turn, then a bounded settle -- were both wrong, and both were theories the evidence then contradicted. This one follows from what the run actually printed. Co-Authored-By: Claude Opus 5 --- tests/ui_registry_observer.hpp | 36 +++++++++++++++++----------------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/tests/ui_registry_observer.hpp b/tests/ui_registry_observer.hpp index a26b823..2fc6001 100644 --- a/tests/ui_registry_observer.hpp +++ b/tests/ui_registry_observer.hpp @@ -79,26 +79,26 @@ class UiRegistryObserver { unsigned count() const { return size_(list_()); } #elif defined(__APPLE__) unsigned listed() const { return static_cast(NSApp ? NSApp.windows.count : 0); } - // AppKit drops a closed window from the application's list when it - // deallocates, which happens on the run loop and takes more than one turn: - // a window counted immediately after close is still listed, while the - // fixture's own bootstrap window is long gone by the time the baseline is - // taken. So let the count settle, bounded, and report whatever it settles - // on -- a window that is genuinely leaked settles too, and is still caught. + // AppKit keeps a closed window in the application's list -- one observed + // here was still held by twenty-four references long after close -- and + // the lifecycle fixture never orders its window in, so neither the list + // nor isVisible separates a live window from a released one the way GTK's + // toplevel registry does. + // + // The provider leaves a marker that does: it installs a delegate when it + // creates a window and clears it when it closes one. Counting its own + // content views with a live delegate therefore asks exactly what the + // fixture means -- has the provider let this window go -- while staying + // independent of the provider's handle map, which is the thing a stale + // handle could otherwise hide. unsigned count() const { - unsigned current = listed(); - unsigned unchanged = 0; - for (int turn = 0; turn < 50 && current; ++turn) { - @autoreleasepool { - [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode - beforeDate:[NSDate dateWithTimeIntervalSinceNow:0.01]]; - } - const unsigned settled = listed(); - unchanged = settled == current ? unchanged + 1 : 0; - current = settled; - if (unchanged >= 3) break; + unsigned owned = 0; + for (NSWindow *window in NSApp.windows) { + if (!window.contentView) continue; + if (![NSStringFromClass([window.contentView class]) isEqualToString:@"NyUiContainer"]) continue; + if (window.delegate) ++owned; } - return current; + return owned; } #elif defined(_WIN32) unsigned count() const {