Repository navigation
Description
🤖 AI-generated issue — filed automatically by the Daily Memory Leak Hunter gh-aw workflow (Pass A — runtime leak hunt). Every claim below was empirically proven by a standalone
dotnet testagainst the shippedMicrosoft.Maui.ControlsNuGet package (no source build, no emulator). Please validate before acting.
Summary
IndicatorView subscribes to its ItemsSource's INotifyCollectionChanged.CollectionChanged with a plain (strong) delegate and only unsubscribes when the ItemsSource property changes. When the bound collection out-lives the IndicatorView — a shared/static/singleton ObservableCollection, or the very collection also bound to a sibling CarouselView/CollectionView — and the page is torn down without ItemsSource being reassigned, the collection's CollectionChanged invocation list keeps the IndicatorView alive. This is the #1 catalog pattern: shared publisher → strong CollectionChanged subscription, no teardown (focus area 4).
Why it retains
IndicatorView.ResetItemsSource(oldItemsSource) runs collection.CollectionChanged += OnCollectionChanged;. The matching -= runs only on the next ItemsSource change (oldCollection.CollectionChanged -= ...). There is no weak collection-changed proxy and no handler-disconnect teardown, so a long-lived collection holds a strong delegate whose Target is the IndicatorView.
This is especially easy to hit because IndicatorView.ItemsSource is documented as "typically bound to the same collection as a CarouselView's ItemsSource" — i.e. a collection whose lifetime is deliberately independent of the indicator.
Retention path
ObservableCollection (long-lived: shared with CarouselView / static / singleton / reused VM)
└─ CollectionChanged (strong delegate)
└─ IndicatorView.OnCollectionChanged ← delegate Target = the IndicatorView
└─ IndicatorView (+ BindingContext, parent Element tree)
Source: src/Controls/src/Core/IndicatorView/IndicatorView.cs:244 (subscribe) — unsubscribe only at :241 (ResetItemsSource(old), property-change path). No weak proxy / no handler-disconnect teardown in the file.
Repro (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:
using System;
using System.Collections.ObjectModel;
using System.Runtime.CompilerServices;
using System.Threading;
using Microsoft.Maui.Controls;
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() { }
}
public class IndicatorViewLeakTest
{
const int N = 30;
sealed class Payload : IndicatorView { public readonly byte[] Data = new byte[1024 * 1024]; }
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;
}
[MethodImpl(MethodImplOptions.NoInlining)]
static WeakReference[] Control()
{
var refs = new WeakReference[N];
for (int i = 0; i < N; i++) refs[i] = new WeakReference(new Payload());
return refs;
}
[MethodImpl(MethodImplOptions.NoInlining)]
static WeakReference[] Leaky(ObservableCollection<string> shared)
{
var refs = new WeakReference[N];
for (int i = 0; i < N; i++) refs[i] = new WeakReference(new Payload { ItemsSource = shared });
return refs;
}
[MethodImpl(MethodImplOptions.NoInlining)]
static WeakReference[] Mitigation(ObservableCollection<string> shared)
{
var refs = new WeakReference[N];
for (int i = 0; i < N; i++) { var v = new Payload { ItemsSource = shared }; v.ItemsSource = null; refs[i] = new WeakReference(v); }
return refs;
}
[Fact]
public void IndicatorView_ItemsSource_Leaks()
{
var shared = new ObservableCollection<string> { "a", "b", "c" };
int control = CountAlive(Control());
int leaky = CountAlive(Leaky(shared));
int mitigation = CountAlive(Mitigation(shared));
GC.KeepAlive(shared);
Assert.Equal(0, control); // constructed IndicatorView, no subscription -> collectable
Assert.Equal(N, leaky); // IndicatorView subscribed to long-lived collection -> retained
Assert.Equal(0, mitigation); // clearing ItemsSource unsubscribes -> collectable
}
}Run:
cd leakprobe && dotnet test --logger "console;verbosity=normal"
Observed results
RESULT IndicatorView_ItemsSource_Leaks: Control=0/30 alive (0 MB) | Leaky=30/30 alive (30 MB) | Mitigation=0/30 alive (0 MB)
| Scenario | Alive / N | Retained |
|---|---|---|
| Control — constructed, never subscribed | 0 / 30 | 0 MB |
| Leaky — subscribed to long-lived collection | 30 / 30 | ~30 MB |
Mitigation — subscribe, then clear ItemsSource |
0 / 30 | 0 MB |
Leaky retaining all 30 while Control and Mitigation release proves the collection's strong CollectionChanged subscription is the sole root.
Affected platforms
All — pure managed cross-platform code in src/Controls/src/Core. No platform handler is needed to reproduce.
Condition: manifests when (a) ItemsSource is an INotifyCollectionChanged (e.g. ObservableCollection) and (b) that collection out-lives the IndicatorView and the page is torn down without reassigning ItemsSource. The documented CarouselView-shared-collection usage makes (b) the normal case.
Suggested fix
Subscribe to CollectionChanged through a weak collection-changed proxy (the repo already has weak-subscription building blocks such as src/Controls/src/Core/Internals/WeakEventProxy.cs, and BindableLayout demonstrates a weak CollectionChanged subscription) or unsubscribe from the collection when the handler disconnects. Either removes the strong root while preserving live-update behavior.
Scope note (honest)
This is a framework weakness with an easy trigger: the control's own documentation steers users toward sharing the collection with a CarouselView, so the leaking configuration is the intended one. Aligning with the weak/handler-teardown pattern used by the item-view controls is the durable fix.
Generated by Daily Memory Leak Hunter · 2.3K AIC · ⌖ 287.2 AIC · ⊞ 49.9K · ◷
Activity
Add AI memory-leak scanner + fixer agentic workflows (#36342)
kubaflo commentedon Jul 12, 2026
🔍 AI-generated cleanup — GitHub Copilot CLI, on behalf of @kubaflo
This
[leak-scan]leak (IndicatorView.ItemsSource) is already fixed onmainby merged #36252. This issue stayed open only because that PR referenced a different (upstream/pre-existing) issue number instead of this[leak-scan]number, so GitHub never auto-closed it.Closing as completed. Note: the fix is on
mainbut may not yet be in a shippedMicrosoft.Maui.ControlsNuGet — if the automated leak hunter re-detects it against the shipped package it may re-file until the fix ships (a known gap being addressed separately). Please reopen if it still reproduces onmain.Add AI memory-leak scanner + fixer agentic workflows (dotnet#36342)