Repository navigation
Description
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 anSKPathBuilderand anSKPath_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
CompositionTargetreleases 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
MartinZikmund commentedon Oct 14, 2026
Details from duplicate #24122 that are not in this issue yet:
CompositionTarget.Rendering.skia.csat 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.SKPathcan be handed to the native clipping layer (on macOSuno_window_clip_svg).ContentPresenter/ContentPresenter_NativeEmbedding_ZIndexsample, or a WebView2/MediaPlayerElement in each), then animate or resize one.