Skip to content

macOS: closing a secondary window frees the NSWindow while managed handles still reference it, causing a later segfault #25326

Closed

Description

@MartinZikmund

Current behavior

On Skia macOS, a closed secondary UNOWindow (NSWindow) is deallocated while managed code and AppKit still reference it. A later autorelease-pool drain on the main thread then releases the freed object and the process dies with Segmentation fault: 11.

This is a pure crash with no managed exception. In CI it hit the macOS WebGPU runtime-test lane in about 16 of 19 runs, always some minutes after a multi-window test closed its secondary window. It also hit the Metal lane once, when a test called Minimize() and then Window.Close() on a secondary window (seen while working on #25203).

Crash signature in every core dump (main thread):

objc_release
 <- AutoreleasePoolPage::releaseUntil
 <- objc_autoreleasePoolPop
 <- -[NSAutoreleasePool drain]
 <- -[NSApplication run]

The fault address is the garbage isa of the freed object plus 0x20 (KERN_INVALID_ADDRESS). The freed pointer is the same value that a managed MacOSNativeOverlappedPresenter._handle and MacOSDragDropExtension._windowHandle still hold, so it is a former secondary window. Window addresses are also reused by later windows, which shows closed windows really are freed.

Expected behavior

Closing a window should leave no dangling native references. The object stays alive until the managed side has dropped its handle, and the app never crashes later because of a window that was closed earlier.

How to reproduce it

Confirmed from CI core dumps and by reading the source. I could not reproduce it locally because I have no Mac available, so the trigger below is the CI pattern, not a verified minimal repro.

  1. Run the Skia macOS desktop head (the crash is much more frequent with UNO_WEBGPU=1).
  2. Open one or more secondary windows, let them render (with WebGPU, a surface gets configured for each), then close them via Window.Close(). Closing a minimized window (Minimize() then Close()) also triggers it.
  3. Keep the app running and do more native-view work on the main thread (in CI: WebView2.ExecuteScriptAsync tests, or full-window MediaPlayerElement tests).
  4. After some time (seconds to minutes) the process segfaults in -[NSApplication run].

Workaround

None in-app. Avoid closing secondary windows on macOS.

Works on UWP/WinUI

N/A (macOS-specific native lifetime bug).

Environment

  • Other
  • Source inspected on master 0599dada9e9; CI core dumps came from a build of 1e8317593f4 (net10.0 desktop, wgpu-native v29.0.1.1)

Affected platforms

Skia (macOS desktop). Windows/Linux not affected.

Anything else

Root cause (partial, confirmed by code inspection on master 0599dada9e9, not by a runtime repro):

  • UNOWindow keeps the NSWindow default releasedWhenClosed = YES under ARC. Nothing in the native project ever sets it to NO.
  • The window is retained only by the static windows set: declared at L13, added at L356 and removed only in -dealloc (L1290). That reference is never dropped on close.
  • Close goes through [window performClose:nil] (L411) and windowWillClose: (L1656), which only removes screen observers and calls back to managed code. AppKit then performs its extra close-time release, which consumes the windows set's reference, so the window is freed while the managed side still holds the raw pointer (MacOSNativeOverlappedPresenter._handle).
  • The release that finally crashes comes from a late autorelease/retain of the dead window by something else. That holder is not pinned (the Metal lane rarely hits it). Candidates: the MTKView used for external present, wgpu's occlusion check (layer.delegate.window.occlusionState), AppKit accessibility/KVO tables, and the Dock tile for minimized windows.

Suggested fix: set releasedWhenClosed = NO in UNOWindow's initializer, detach the rendering view in windowWillClose:, and drop the windows reference asynchronously after close (for example via dispatch_async on the main queue), so nothing frees a window that managed code still points at. A fix is in progress and still needs a CI run of the macOS WebGPU lane to confirm.

Related: #25203 (a PR whose new close-a-minimized-window test hit this crash on the Metal lane; a symptom there, not the same change).

Activity

  1. added
    kind/bug
    platform/macos 🍏
    triage/untriaged
    difficulty/challenging 🤯
    area/windowing
    on Oct 12, 2026
  2. MartinZikmund commented on Oct 12, 2026

    @MartinZikmund
    MemberAuthor

    A fix is in progress in #25201.

  3. added
    7.0
    on Oct 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    7.0area/skia/stability ✏️area/windowingdifficulty/challenging 🤯kind/bugplatform/macos 🍏renderer/skiatriage/untriaged

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions