Skip to content

ContainerVisual allocates a weak GCHandle per instance that is never freedΒ #24184

Open

Description

@MartinZikmund

Current behavior πŸ›

Every ContainerVisual allocates a weak GCHandle in its constructor and nothing ever frees it.

src/Uno.UI.Composition/Composition/ContainerVisual.skia.cs:

private GCHandle _gcHandle;          // line 24
…
_gcHandle = GCHandle.Alloc(this, GCHandleType.Weak);   // line 67
Handle = GCHandle.ToIntPtr(_gcHandle);                 // line 68

_gcHandle appears at exactly those three lines and nowhere else in the repository β€” there is no
Free() call anywhere for it.

The disposal path does not cover it either. CompositionObject is IDisposable and has a finalizer:

~CompositionObject() => Dispose();
public void Dispose() => DisposeInternal();
private protected virtual void DisposeInternal() { /* StopAllAnimations only */ }

ContainerVisual does not override DisposeInternal (the only override in the assembly is
VisualInteractionSource), so neither Dispose() nor finalization releases the handle. Collecting
the ContainerVisual clears the weak handle's target but leaves the handle slot allocated for the
lifetime of the process
.

Every UIElement has a Visual, so the handle table grows monotonically with the number of visuals
a process has ever created. In a long-lived process this is unbounded; the runtime-test host creates
visuals for ~8100 tests in a single run.

Expected behavior 🎯

The handle is freed when the visual is disposed or finalized, so the handle table tracks live visuals
rather than every visual ever created.

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

By inspection of the three lines above; there is no Free() to find. To observe the growth, create
and drop visuals in a loop and watch the GC handle count (dotnet-counters monitor --counters System.Runtime shows GC Handle Count), or take two dumps and compare handle-table size.

I have not measured the runtime cost, and I would not assume it is large: a weak handle is cheap
individually, and the impact is per-GC handle-table work proportional to the count. Filing this on the
correctness of the lifetime rather than on a measured regression.

Workaround πŸ› οΈ

None available to consumers.

Renderer 🎨

Skia

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

All Skia targets β€” the file is ContainerVisual.skia.cs, so Desktop (Windows/macOS/Linux), Skia on
Android/iOS, and WebAssembly.

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

Present on feature/breakingchanges.

IDE version πŸ§‘β€πŸ’»

N/A

Anything else we need to know? πŸ’¬

Found while investigating the macOS runtime-test job for #24005; it is not established as a cause
of anything there β€” the instrumented runs show gcPauseDelta=0ms during the freezes, so GC is
explicitly ruled out as the freeze mechanism. Filing it on its own merits.

Worth deciding alongside this: Handle is handed to native code (for example
MacOSAccessibility passes visual.Handle to uno_accessibility_add_element), so freeing the handle
needs to be sequenced against native code that may still hold the IntPtr. That is the real design
question here, and it is why this is filed rather than fixed blind.

A second, smaller smell in the same disposal path: ~CompositionObject() calls Dispose(), which
can reach DispatcherQueue.Main.TryEnqueue(StopAllAnimations) β€” scheduling work from a finalizer.

Activity

  1. added
    triage/untriaged
    on Aug 24, 2026
  2. added
    7.1
    on Oct 14, 2026
  3. 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