From 9d0d8b14520f7fe98b45f670975cfccb57b4798a Mon Sep 17 00:00:00 2001 From: Nathan Young Date: Sun, 8 Nov 2026 23:05:07 -0500 Subject: [PATCH] Let the AppKit window count settle before judging release The counts asked for in the last run answered the question: "to baseline 0 (listed 1, of those visible 0)". The window was closed, not leaked, and AppKit simply had not dropped it from the application's list yet. The same line carried the proof, because the fixture's own bootstrap window is created and closed before the baseline is taken and the baseline was 0 -- closed windows do leave the list, just not within the single run-loop turn the observer was giving them. count() now lets the figure settle, bounded to fifty ten-millisecond turns and stopping as soon as it holds steady three times. A genuinely leaked window settles too and is still reported, so this waits for AppKit's bookkeeping without weakening what the assertion means. detail() reports the unsettled list, since its job is to say what was actually there. The in-process input probe passes: text, toggle and click all arrive with the entry reading "Matrix", 41 checks, which is real keyboard input reaching each control and becoming a provider event on macOS. Co-Authored-By: Claude Opus 5 --- tests/ui_registry_observer.hpp | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/tests/ui_registry_observer.hpp b/tests/ui_registry_observer.hpp index fd49152..b1ba7d8 100644 --- a/tests/ui_registry_observer.hpp +++ b/tests/ui_registry_observer.hpp @@ -78,16 +78,27 @@ class UiRegistryObserver { #if defined(__linux__) 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. unsigned count() const { - // AppKit completes window teardown on the run loop, so a window the - // provider has already released is still listed if it is counted in the - // same turn. Give the loop one bounded, nonblocking turn first; this is - // the observer accommodating AppKit's bookkeeping, not the provider - // holding anything. - @autoreleasepool { - [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantPast]]; + 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; } - return static_cast(NSApp ? NSApp.windows.count : 0); + return current; } #elif defined(_WIN32) unsigned count() const { @@ -104,7 +115,7 @@ class UiRegistryObserver { #if defined(__APPLE__) unsigned shown = 0; for (NSWindow *window in NSApp.windows) if (window.visible) ++shown; - return " (listed " + std::to_string(count()) + ", of those visible " + std::to_string(shown) + ")"; + return " (listed " + std::to_string(listed()) + ", of those visible " + std::to_string(shown) + ")"; #else return " (observed " + std::to_string(count()) + ")"; #endif