Skip to content

CompositionTarget never releases its native Skia objects, and caches the native clip path in static fieldsΒ #24239

Open

Description

@MartinZikmund

Current behavior πŸ›

Microsoft.UI.Xaml.Media.CompositionTarget (Skia) owns a number of native SkiaSharp objects for the lifetime of a window and never releases any of them. It does not implement IDisposable, and OnTargetUnregistered β€” its only teardown hook β€” removes the target from the tracking maps without touching them:

private static void OnTargetUnregistered(CompositionTarget target)
{
    _targets.Remove(target);
    if (_latestFrames.Remove(target, out var picture))
    {
        picture.Release(pictureAccessed: false);
    }
}

Per instance (src/Uno.UI/UI/Xaml/Media/CompositionTarget.Rendering.cs):

  • _pendingDamage β€” owns an SKPathBuilder and an SKPath
  • _damageSnapshotPool β€” Stack<SKPath>
  • _damageOutsetPaint (SKPaint), _damageOutsetBandBuilder (SKPathBuilder), _outsetDamage (SKPath)
  • the SKPaths held by _lastRenderedFrame

Static:

  • _lastNativeClipPath, _lastScaledNativeClipPath

There are two separate problems here.

1. Release is tied to GC rather than to window close.
SKNativeObject declares a finalizer, so the handles are reclaimed β€” this is not an unbounded leak, and the per-target amount is small (~185 B managed plus two native handles for _pendingDamage). But nothing is released deterministically when a window closes, and the total scales with how many windows an app opens over a session.

2. The scaled native-element clip path is cached in static fields but written per window.
_lastNativeClipPath and _lastScaledNativeClipPath are static, yet they are read and written from Draw(), which runs on a dedicated render thread per window. With two windows rendering concurrently, each can observe and overwrite the other's cache β€” and both can write the same native SKPath at the same time. These two fields logically belong with the per-instance _lastCanvasSize / _lastRasterizationScale pair they sit next to.

Expected behavior 🎯

  • A CompositionTarget releases all of its native Skia objects together when its window is torn down, guarded so that a late invalidation cannot reach a released object.
  • The native-element clip path cache is per instance, so concurrent windows cannot corrupt each other.

How to reproduce it (as minimally and precisely as possible) πŸ”¬

Not reproducible as a user-visible failure today β€” found by inspection while reviewing a damage-region change. Problem 2 requires two Skia desktop windows rendering concurrently.

Workaround πŸ› οΈ

None required for problem 1 β€” the finalizer reclaims the memory.

Please do not "fix" this by disposing individual fields in OnTargetUnregistered. ICompositionTarget.AddDamage is reachable from ordinary visual invalidation on the UI thread, while OnTargetUnregistered runs from the XamlRootMap.Unregistered handler. Disposing there, without first guaranteeing that no further invalidation can arrive, converts a harmless delayed reclaim into a hard process kill β€” calling into a disposed SKPathBuilder is an access violation, not an ObjectDisposedException:

Fatal error. 0xC0000005
   at SkiaSharp.SkiaApi.sk_pathbuilder_add_rect(IntPtr, SkiaSharp.SKRect*, SkiaSharp.SKPathDirection)
   at SkiaSharp.SKPathBuilder.AddRect(SkiaSharp.SKRect, SkiaSharp.SKPathDirection)

(Verified against SkiaSharp 4.151.1 on .NET 10.) The fix needs to be a real teardown path that releases everything together and makes the post-unregister invalidation path safe, validated at runtime on window close.

Renderer 🎨

  • Skia

Affected platforms πŸ“±πŸ’»πŸ–₯️

Desktop (Windows), Desktop (macOS), Desktop (X11) β€” the lifetime issue applies to every Skia head; the concurrent-window aspect of problem 2 is desktop-only.

Uno.Sdk version (and other relevant versions) πŸ“¦

Framework source; observed on feature/breakingchanges (7.0 development). SkiaSharp 4.151.1.

Anything else we need to know? πŸ’¬

Surfaced while reviewing a damage-accumulation change that swaps _pendingDamage from an undisposed SKPath to an undisposed DamageRegion. That change leaves the lifetime exactly as it was, so this is pre-existing and is tracked here rather than folded into an unrelated performance PR.

Activity

  1. added
    triage/untriaged
    on Aug 27, 2026
  2. MartinZikmund commented on Oct 14, 2026

    @MartinZikmund
    MemberAuthor

    Details from duplicate #24122 that are not in this issue yet:

    • Precise locations (CompositionTarget.Rendering.skia.cs at the time): fields at :59-62. A per-target canvas recreation nulls the scaled path at :304. The cache is keyed on this target's frame at :348 (_lastNativeClipPath != lastRenderedFrame.nativeElementClipPath). Writes at :350/:354 and return at :359.
    • Consequences:
      • Recreating window A's canvas nulls window B's cached path.
      • Two windows with different clip paths invalidate each other every frame.
      • A torn or empty SKPath can be handed to the native clipping layer (on macOS uno_window_clip_svg).
    • Repro: open two windows that host native elements with different clip geometry (e.g. two instances of the ContentPresenter/ContentPresenter_NativeEmbedding_ZIndex sample, or a WebView2/MediaPlayerElement in each), then animate or resize one.
    • Affects Win32, X11, and macOS once feat(macos): Dedicated Metal render threadΒ #23594 lands.
  3. added
    7.1
    on Oct 14, 2026
  4. removed
    7.1
    on Oct 29, 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

    triage/untriaged

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions