Skip to content

[leak-scan] IndicatorView.ItemsSource: observable ItemsSource strongly roots the IndicatorView via CollectionChanged (no teardow [Content truncated due to length] #36352

Closed

Description

@github-actions

🤖 AI-generated by the Daily Memory Leak Hunter agentic workflow for dotnet/maui. Filed automatically after an empirical dotnet test confirmed the leak against the shipped Microsoft.Maui.Controls NuGet package (v10.0.0, plain net10.0, no emulator, no source build). Please verify before acting.

Summary

IndicatorView.ItemsSource, when set to a collection implementing INotifyCollectionChanged, subscribes directly (non-weak) to CollectionChanged and detaches only when ItemsSource is reassigned — never on unload/teardown. Because an IndicatorView is, by design, "typically bound to the same collection as a CarouselView's ItemsSource" (see the property's own doc comment), that source is normally a long-lived VM collection — so the collection strongly roots the IndicatorView (and its page) for the collection's lifetime.

Retention path

ObservableCollection (shared with a CarouselView / VM)
  └─ CollectionChanged (plain CLR event)
       └─ delegate target = IndicatorView.OnCollectionChanged
            └─ IndicatorView  ──BindingContext──▶ (page / payload)
  • Subscribe (strong): src/Controls/src/Core/IndicatorView/IndicatorView.cs:244 — collection.CollectionChanged += OnCollectionChanged;
  • Only detach path: src/Controls/src/Core/IndicatorView/IndicatorView.cs:241 — unsubscribes the old value on ItemsSource change only. No unload/teardown detach.

Observed results (empirical)

Environment: .NET SDK 10.0.301 · Microsoft.Maui.Controls 10.0.0 · target net10.0 · Linux · no emulator. N = 30, 1 MB payload each, full GC before measuring.

Scenario Description Live subjects (of N=30) Retained
Control bare IndicatorView, no ItemsSource 0 ~0 MB
Leaky ItemsSource = shared observable, no teardown 30 ~30 MB
Mitigation same, then ItemsSource = null before release 0 ~0 MB

Repro (standalone — restores the shipped package, runs on plain net10.0)

nuget.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <packageSources>
    <clear />
    <add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
  </packageSources>
</configuration>

leakprobe.csproj:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net10.0</TargetFramework>
    <Nullable>enable</Nullable>
    <ImplicitUsings>disable</ImplicitUsings>
    <IsPackable>false</IsPackable>
    <LangVersion>latest</LangVersion>
    <NoWarn>$(NoWarn);CS0618;CS0612;CS0619</NoWarn>
  </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;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Runtime.CompilerServices;
using Microsoft.Maui.Controls;
using Xunit;

namespace LeakProbe;

public class LeakTest
{
    const int N = 30;

    [MethodImpl(MethodImplOptions.NoInlining)]
    static (object shared, List<WeakReference> refs) Alloc(Func<object> makeShared, Func<object, BindableObject> make)
    {
        var shared = makeShared();
        var refs = new List<WeakReference>(N);
        for (int i = 0; i < N; i++)
        {
            var payload = new byte[1024 * 1024];
            var control = make(shared);
            control.BindingContext = payload;
            refs.Add(new WeakReference(payload));
        }
        return (shared, refs);
    }

    static void ForceGC()
    {
        for (int i = 0; i < 6; i++) { GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); }
    }

    static int Alive(List<WeakReference> refs)
    {
        int n = 0;
        foreach (var r in refs) if (r.IsAlive) n++;
        return n;
    }

    static void Check(Func<object> makeShared, Func<object, BindableObject> control,
                      Func<object, BindableObject> leaky, Func<object, BindableObject> mitigation)
    {
        var c = Alloc(makeShared, control);
        var l = Alloc(makeShared, leaky);
        var m = Alloc(makeShared, mitigation);
        ForceGC();
        int controlAlive = Alive(c.refs), leakyAlive = Alive(l.refs), mitigationAlive = Alive(m.refs);
        GC.KeepAlive(c.shared); GC.KeepAlive(l.shared); GC.KeepAlive(m.shared);
        Assert.Equal(0, controlAlive);
        Assert.Equal(N, leakyAlive);
        Assert.Equal(0, mitigationAlive);
    }

    [Fact]
    public void IndicatorView_ItemsSource_Leaks() => Check(
        makeShared: () => new ObservableCollection<string> { "a", "b", "c" },
        control:    _ => new IndicatorView(),
        leaky:      s => { var v = new IndicatorView(); v.ItemsSource = (IEnumerable)s; return v; },
        mitigation: s => { var v = new IndicatorView(); v.ItemsSource = (IEnumerable)s; v.ItemsSource = null; return v; });
}

Run:

dotnet test --logger "console;verbosity=normal"

Affected platforms & conditions

Managed cross-platform code → all platforms. Default behavior when ItemsSource is an observable collection that outlives the control — which is the intended usage (shared with a CarouselView), making this leak especially likely in real apps.

Suggested fix

Subscribe through WeakNotifyCollectionChangedProxy (src/Controls/src/Core/Internals/WeakEventProxy.cs:65) instead of += OnCollectionChanged, or detach on unloaded/teardown.

Scope note

Genuine framework retention with pure BCL types; no misuse required. The shared-source usage is explicitly the documented pattern, so this is not a footgun but a framework gap.


Confirmed alongside 5 other distinct managed leaks in a single dotnet test run this scan; each is filed as its own [leak-scan] issue.

Generated by Daily Memory Leak Hunter · 1.9K AIC · ⌖ 348.3 AIC · ⊞ 47.2K · ◷

Activity

  1. added
    perf/memory-leak 💦
    on Jul 4, 2026
  2. kubaflo commented on Jul 12, 2026

    @kubaflo
    Contributor

    🔍 AI-generated cleanup — GitHub Copilot CLI, on behalf of @kubaflo

    This [leak-scan] leak (IndicatorView.ItemsSource) is already fixed on main by 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 main but may not yet be in a shipped Microsoft.Maui.Controls NuGet — 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 on main.

  3. locked and limited conversation to collaborators on Aug 12, 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