Repository navigation
Description
🤖 AI-generated by the Daily Memory Leak Hunter agentic workflow for
dotnet/maui. Filed automatically after an empiricaldotnet testconfirmed the leak against the shippedMicrosoft.Maui.ControlsNuGet package (v10.0.0, plainnet10.0, no emulator, no source build). Please verify before acting.
Summary
SwipeItemView subscribes directly to its Command's CanExecuteChanged and detaches only when Command is reassigned — never on unload/teardown. When the ICommand implementation exposes CanExecuteChanged as a plain (non-weak) CLR event — the default when you implement ICommand yourself, and the case for many third-party MVVM commands — the shared/long-lived command strongly roots the SwipeItemView and its page.
Retention path
ICommand (shared, VM-lived; plain-event CanExecuteChanged)
└─ CanExecuteChanged
└─ delegate target = SwipeItemView.OnCommandCanExecuteChanged
└─ SwipeItemView ──BindingContext──▶ (page / payload)
- Subscribe (strong):
src/Controls/src/Core/SwipeView/SwipeItemView.cs:62—Command.CanExecuteChanged += OnCommandCanExecuteChanged; - Only detach path:
src/Controls/src/Core/SwipeView/SwipeItemView.cs:70—OnCommandChangingdetaches onCommandchange 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. The shared command is a minimal ICommand with a plain event EventHandler CanExecuteChanged (models a hand-rolled / third-party RelayCommand).
| Scenario | Description | Live subjects (of N=30) | Retained |
|---|---|---|---|
| Control | bare SwipeItemView, no Command |
0 | ~0 MB |
| Leaky | Command = shared non-weak ICommand, no teardown |
30 | ~30 MB |
| Mitigation | same, then Command = 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.Generic;
using System.Runtime.CompilerServices;
using System.Windows.Input;
using Microsoft.Maui.Controls;
using Xunit;
namespace LeakProbe;
public class LeakTest
{
const int N = 30;
// Plain-event ICommand (NOT MAUI's WeakEventManager-backed Command) — models a
// hand-rolled or third-party RelayCommand whose CanExecuteChanged is a normal CLR event.
sealed class LeakyCommand : ICommand
{
public event EventHandler? CanExecuteChanged;
public bool CanExecute(object? p) => true;
public void Execute(object? p) { }
}
[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 SwipeItemView_Command_Leaks() => Check(
makeShared: () => new LeakyCommand(),
control: _ => new SwipeItemView(),
leaky: s => new SwipeItemView { Command = (ICommand)s },
mitigation: s => { var v = new SwipeItemView { Command = (ICommand)s }; v.Command = null; return v; });
}Run:
dotnet test --logger "console;verbosity=normal"
Affected platforms & conditions
Managed cross-platform code → all platforms. Non-default condition: requires an ICommand whose CanExecuteChanged is a plain CLR event.
Suggested fix
Subscribe to CanExecuteChanged via the existing WeakCommandSubscription (src/Controls/src/Core/WeakCommandSubscription.cs:8), as other command-driven controls do.
Scope note
Honest scope: MAUI's built-in Command routes CanExecuteChanged through a WeakEventManager (src/Controls/src/Core/Command.cs), so the built-in command does not leak here. This is a framework-hardening gap against non-weak ICommand implementations (common in third-party MVVM), not a leak with the framework Command. It is nonetheless empirically real and cheaply fixed with the weak subscription helper the framework already ships.
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
kubaflo commentedon Jul 12, 2026
🔍 AI-generated cleanup — GitHub Copilot CLI, on behalf of @kubaflo
This
[leak-scan]leak (SwipeItemView.Command) is already fixed onmainby merged #36357. 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.