Repository navigation
Description
Important
This issue was AI-generated by the Daily Memory Leak Hunter — dotnet/maui workflow.
Description
Assigning a shared PathSegmentCollection to PathFigure.Segments creates a strong CollectionChanged subscription from the shared collection to the transient PathFigure. If the collection outlives the figure, the figure and its binding context remain rooted unless Segments is explicitly replaced.
Retention path
shared PathSegmentCollection -> CollectionChanged delegate -> PathFigure.OnPathSegmentCollectionChanged -> PathFigure -> BindingContext
The subscription is established in src/Controls/src/Core/Shapes/PathFigure.cs:99-109; replacing the collection removes it at line 101.
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 PathFigure_Segments_Leaks()
{
var shared = new PathSegmentCollection();
var control = Enumerable.Range(0, N).Select(_ => Create(() => new PathFigure())).ToArray();
var leaky = Enumerable.Range(0, N).Select(_ => Create(() => new PathFigure { Segments = shared })).ToArray();
var mitigation = Enumerable.Range(0, N).Select(_ => Create(() =>
{
var figure = new PathFigure { Segments = shared };
figure.Segments = new PathSegmentCollection();
return figure;
})).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<PathFigure> 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 (Segments 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 PathSegmentCollection is shared or otherwise long-lived and the figure is discarded without replacing Segments.
Suggested fix
Subscribe through a weak collection-changed proxy (with finalizer cleanup), or otherwise ensure a discarded PathFigure 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 Segments 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 #38718.
Same shared PathSegmentCollection.CollectionChanged -> PathFigure.OnPathSegmentCollectionChanged -> figure/binding context. Both retain otherwise unreachable figures until Segments is explicitly replaced; identical missing unsubscribe mechanism.