Skip to content

macOS native element peers are destroyed on Unloaded, so reparenting is unsupported #24172

Open

Description

@MartinZikmund

Current behavior 🐛

On Skia macOS, a native element's peer is destroyed when the element leaves the visual tree, not when its owner is disposed. MacOSNativeElement calls DisposeNativePeer() from its Unloaded handler, and Unloaded fires on any tree leave — including a reparent, which is immediately followed by Loaded.

The result is that a native-hosted control cannot be reparented on macOS. Every operation after the move targets a peer that no longer exists.

macOS is the only backend that does this:

Backend DetachNativeElement behaviour
X11 Reparents the X window to root and unmaps it — the window survives
Win32 Hides the HWND and calls SetParent(HWND.Null); a code comment states "WS_CHILD stays set across detach/re-attach cycles" — re-attach is explicitly supported
macOS Destroys the peer

This is compounded by ContentPresenter already pairing EnterImpl → AttachNativeElement with LeaveImpl → DetachNativeElement, and uno_native_detach already parking the view in transients precisely so that it survives. The Unloaded hook is a second, destructive teardown on the same transition.

Expected behavior 🎯

Detaching a native element parks its peer so a subsequent attach can reuse it, matching X11 and Win32. Reparenting a WebView2 or MediaPlayerElement on macOS should preserve the peer, not destroy it.

How to reproduce it (as minimally and precisely as possible) 🔬

Reparent a native-hosted element on Skia macOS:

border.Child = null;        // Unloaded -> DisposeNativePeer()
border.Child = webView;     // Loaded, but the peer is gone
await webView.CoreWebView2.NavigateToString(html);   // operates on a destroyed peer

Covered by the existing runtime tests When_WebMessageReceived_After_RemoveAdd and When_MediaPlayerElement_SetIsFullWindow_Check_Fullscreen.

Before #24112 this was a hard SIGSEGV in libUnoNativeMac that took down the whole test host. #24112 makes it a logged no-op, so the operation now fails instead of crashing the process — but the underlying lifetime defect is untouched.

Workaround 🛠️

None. Avoid reparenting native-hosted elements on macOS.

Why the Unloaded hook cannot simply be deleted

Two pieces are missing, which is why #24112 did not attempt it:

  1. uno_native_attach never removes the element from transients, so that static set only ever grows. The Unloaded dispose is currently the only thing draining it. Removing the hook without a replacement trades a crash for an unbounded NSView leak across every macOS-hosted control.
  2. There is no owner-driven disposal signal. CoreWebView2 has no Dispose, only the tree-scoped OnLoaded/OnUnloaded, so the peer's lifetime is effectively the GC lifetime of the WebView2 element.

A correct fix therefore needs uno_native_attach to drain transients when an element re-enters elements, plus a real disposal signal from the owner — a new lifetime contract rather than a patch.

Renderer 🎨

Skia

Affected platforms 📱💻🖥️

macOS (Skia)

Anything else we need to know? 💬

Analysis by @Xiaoy312 while triaging the macOS Segmentation fault: 11 CI failures that led to #24112. Related latent instance of the same class of bug: the MacOSMediaPlayerPresenterExtension shadow-handle issue filed separately.

Activity

  1. added
    platform/macos 🍏
    and removed on Oct 9, 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

    kind/bugplatform/macos 🍏

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions