Repository navigation
Description
Important
This issue was AI-generated by the Daily Memory Leak Hunter — dotnet/maui workflow.
Description
Assigning a shared TransformCollection to TransformGroup.Children creates a strong CollectionChanged subscription from the shared collection to the transient TransformGroup. If the collection outlives the group, the group and its binding context remain rooted unless Children is explicitly replaced.
Retention path
shared TransformCollection -> CollectionChanged delegate -> TransformGroup.OnChildrenCollectionChanged -> TransformGroup -> BindingContext
The subscription is established in src/Controls/src/Core/Shapes/TransformGroup.cs:44-60; replacing the collection removes it at lines 67-75.
Standalone repro
leakprobe.csproj:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
</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:
using System;
using System.Linq;
using System.Runtime.CompilerServices;
using Microsoft.Maui.Controls;
using Microsoft.Maui.Controls.Shapes;
using Xunit;
public class LeakTest
{
const int N = 20;
[Fact]
public void TransformGroup_Children_Leaks()
{
var shared = new TransformCollection();
var control = Enumerable.Range(0, N).Select(_ => Create(() => new TransformGroup())).ToArray();
var leaky = Enumerable.Range(0, N).Select(_ => Create(() => new TransformGroup { Children = shared })).ToArray();
var mitigation = Enumerable.Range(0, N).Select(_ => Create(() =>
{
var group = new TransformGroup { Children = shared };
group.Children = new TransformCollection();
return group;
})).ToArray();
ForceGc();
Assert.Equal(0, control.Count(x => x.IsAlive));
Assert.Equal(0, mitigation.Count(x => x.IsAlive));
Assert.Equal(N, leaky.Count(x => x.IsAlive));
GC.KeepAlive(shared);
}
[MethodImpl(MethodImplOptions.NoInlining)]
static WeakReference Create(Func<TransformGroup> factory)
{
var subject = factory();
subject.BindingContext = new Payload();
return new WeakReference(subject);
}
static void ForceGc()
{
for (var i = 0; i < 7; i++)
{
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
}
}
sealed class Payload
{
readonly byte[] bytes = new byte[1024 * 1024];
}
}Run:
dotnet test --logger "console;verbosity=normal"Observed results
| Scenario | Alive | Retained payload |
|---|---|---|
| Control | 0/20 | 0 MB |
Mitigation (Children replaced) |
0/20 | 0 MB |
| Leaky (shared collection retained) | 20/20 | 20 MB |
Scope
This is purely managed code and affects all platforms. It occurs when a TransformCollection is shared or otherwise long-lived and the group is discarded without replacing Children.
Suggested fix
Subscribe through a weak collection-changed proxy (with finalizer cleanup), or otherwise ensure a discarded TransformGroup is not strongly held by the shared collection.
This is a framework hardening issue: sharing bindable collection values is supported, and requiring callers to replace Children solely for memory cleanup is an easy-to-miss usage footgun.
Generated by Daily Memory Leak Hunter · gpt56 · 289 AIC · ⌖ 15.8 AIC · ⊞ 28K · ◷
Activity
kubaflo commentedon Oct 6, 2026
Duplicate of #38717.
Same shared TransformCollection.CollectionChanged -> TransformGroup.OnChildrenCollectionChanged -> group/binding context. Both detach only when Children is replaced; this is the same shared-collection root rather than a separate child-transform mutation bug.