Skip to content

[leak-scan] TransformGroup.Children β€” shared TransformCollection strongly roots the TransformGroup via CollectionChanged (no unl [Content truncated due to length]Β #36367

Closed as not planned

Description

@github-actions

πŸ€– AI-generated issue β€” filed automatically by the Daily Memory Leak Hunter agentic workflow for dotnet/maui. It reports only leaks it empirically confirmed this run with a standalone dotnet test against the shipped Microsoft.Maui.Controls NuGet package (no source build, no emulator). One of 6 distinct leaks confirmed this run that share the same mechanism. Please validate before acting.

Summary

TransformGroup subscribes its instance method OnChildrenCollectionChanged to the assigned Children (TransformCollection) CollectionChanged (in OnTransformGroupChanged). The subscription is removed only when Children is reassigned. A shared / long-lived TransformCollection therefore roots the TransformGroup β€” and any element using it as RenderTransform β€” for as long as the collection lives.

Why it retains

Strong CollectionChanged instance-delegate subscription, torn down only in the oldValue branch of OnTransformGroupChanged (i.e. on reassignment). No unload / detach teardown.

Retention path

TransformCollection (long-lived)
  └─ CollectionChanged += group.OnChildrenCollectionChanged  ──► TransformGroup
  • Subscribe (strong): src/Controls/src/Core/Shapes/TransformGroup.cs:44
  • Teardown only on reassignment: src/Controls/src/Core/Shapes/TransformGroup.cs:39
  • Property + change handler: src/Controls/src/Core/Shapes/TransformGroup.cs:14, :35

Repro (standalone β€” shipped package, plain net10.0, no device)

leakprobe.csproj:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net10.0</TargetFramework>
    <Nullable>enable</Nullable>
    <IsPackable>false</IsPackable>
    <LangVersion>latest</LangVersion>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.Maui.Controls" Version="10.0.0" />
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
    <PackageReference Include="xunit" Version="2.9.2" />
    <PackageReference Include="xunit.runner.visualstudio" Version="2.8.2" />
  </ItemGroup>
</Project>

LeakTest.cs (TransformGroup is sealed, so the 1 MB payload is attached via BindingContext β€” a strong reference from the BindableObject β€” and the group itself is tracked):

using System;
using System.Runtime.CompilerServices;
using System.Threading;
using Microsoft.Maui.Controls;
using Microsoft.Maui.Controls.Shapes;
using Microsoft.Maui.Dispatching;
using Xunit;

namespace LeakProbe;

// Dispatcher stub so MAUI controls can be constructed on plain net10.0 (no platform).
static class TestInit
{
	[ModuleInitializer]
	public static void Init() => DispatcherProvider.SetCurrent(new StubDispatcherProvider());
}
sealed class StubDispatcherProvider : IDispatcherProvider
{
	readonly ThreadLocal<IDispatcher> _d = new(() => new StubDispatcher());
	public IDispatcher? GetForCurrentThread() => _d.Value;
}
sealed class StubDispatcher : IDispatcher
{
	public bool IsDispatchRequired => false;
	public bool Dispatch(Action action) { action(); return true; }
	public bool DispatchDelayed(TimeSpan delay, Action action) => true;
	public IDispatcherTimer CreateTimer() => new StubTimer();
}
sealed class StubTimer : IDispatcherTimer
{
	public TimeSpan Interval { get; set; }
	public bool IsRepeating { get; set; }
	public bool IsRunning => false;
	public event EventHandler? Tick;
	public void Start() { _ = Tick; }
	public void Stop() { }
}

static class Gc
{
	public const int N = 30;
	public static int CountAlive(WeakReference[] refs)
	{
		for (int i = 0; i < 8; i++) { GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); }
		int alive = 0;
		foreach (var r in refs) if (r.IsAlive) alive++;
		return alive;
	}
}

public class TransformGroupChildrenLeakTest
{
	static TransformGroup Make() => new TransformGroup { BindingContext = new byte[1024 * 1024] };

	[MethodImpl(MethodImplOptions.NoInlining)]
	static WeakReference[] Control()
	{
		var refs = new WeakReference[Gc.N];
		for (int i = 0; i < Gc.N; i++) refs[i] = new WeakReference(Make());
		return refs;
	}
	[MethodImpl(MethodImplOptions.NoInlining)]
	static WeakReference[] Leaky(TransformCollection children)
	{
		var refs = new WeakReference[Gc.N];
		for (int i = 0; i < Gc.N; i++) { var v = Make(); v.Children = children; refs[i] = new WeakReference(v); }
		return refs;
	}
	[MethodImpl(MethodImplOptions.NoInlining)]
	static WeakReference[] Mitigation(TransformCollection children)
	{
		var refs = new WeakReference[Gc.N];
		for (int i = 0; i < Gc.N; i++)
		{
			var v = Make();
			v.Children = children;
			v.Children = new TransformCollection(); // reassign detaches the shared collection
			refs[i] = new WeakReference(v);
		}
		return refs;
	}

	[Fact]
	public void TransformGroup_Children_Leaks()
	{
		var children = new TransformCollection();
		int control = Gc.CountAlive(Control());
		int leaky = Gc.CountAlive(Leaky(children));
		int mitigation = Gc.CountAlive(Mitigation(children));
		GC.KeepAlive(children);
		Assert.Equal(0, control);
		Assert.Equal(Gc.N, leaky);
		Assert.Equal(0, mitigation);
	}
}

Run:

cd leakprobe && dotnet test --logger "console;verbosity=normal"

Observed results (test PASSES β†’ leak confirmed)

Scenario Description Instances alive after full GC Retained
Control No shared collection 0 / 30 ~0 MB
Leaky One shared TransformCollection kept alive 30 / 30 ~30 MB
Mitigation Children reassigned per-instance 0 / 30 ~0 MB

Affected platforms

All β€” purely managed cross-platform code (src/Controls/src/Core/Shapes): Android, iOS, MacCatalyst, Windows, Tizen.

Non-default condition: requires a TransformCollection that out-lives the group (shared / static / cached transform) and no reassignment.

Suggested fix

Subscribe via WeakEventManager, or detach CollectionChanged / per-child PropertyChanged on unload / handler disconnect rather than only on collection reassignment.

Scope note

Usage footgun (needs the collection to out-live the group) but a genuine framework inconsistency β€” strong subscription, no unload teardown β€” matching the shape already filed for Picker.ItemsSource et al. Sharing a TransformCollection (e.g. a reused RenderTransform) is plausible.

Generated by Daily Memory Leak Hunter Β· 1.9K AIC Β· βŒ– 314.3 AIC Β· ⊞ 47.7K Β· β—·

Activity

  1. added
    perf/memory-leak πŸ’¦
    on Jul 4, 2026
  2. added a commit that references this issue on Jul 12, 2026
    66434a3
  3. added a commit that references this issue on Jul 16, 2026
    6da3e40
  4. added 2 commits that reference this issue on Jul 17, 2026
    242e476
    bf8616e
  5. kubaflo commented on Aug 14, 2026

    @kubaflo
    Contributor

    Closing this automated leak-scan issue as bot-generated tracker noise. The full report, links, labels, and comments have been archived so the underlying test, infrastructure, or workflow problem can still be investigated without keeping the product issue backlog open.

    Disclosure: this comment was generated by AI.

  6. locked and limited conversation to collaborators on Sep 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

    agentic-workflowsperf/memory-leak πŸ’¦

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions