Skip to content

[leak-scan] VisualStateGroupList.Clear — removed groups keep triggers attached #38245

Closed

Description

@github-actions

Important

AI-generated by the Daily Memory Leak Hunter — dotnet/maui workflow. This finding was empirically confirmed against the shipped Microsoft.Maui.Controls 10.0.0 package on plain net10.0.

Description

Clearing an attached VisualStateGroupList removes groups without detaching the state triggers nested inside them. A trigger subscribed to a shared managed publisher in OnAttached() therefore stays subscribed and retained after its group is removed.

Retention path

shared publisher -> event delegate -> StateTriggerBase in removed VisualStateGroup -> page-local payload

  • src/Controls/src/Core/VisualStateManager.cs:350-358 clears the group list and removes only StatesChanged handlers; it does not detach nested triggers.
  • src/Controls/src/Core/VisualStateManager.cs:373-419 similarly omits trigger teardown for Remove, RemoveAt, and index replacement.
  • src/Controls/src/Core/StateTriggerBase.cs:52-75 unsubscribes only when SendDetached() reaches OnDetached().

Standalone repro

The reflection calls simulate the framework's internal attach/detach lifecycle without requiring a platform window.

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.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using Microsoft.Maui.Controls;
using Xunit;

public sealed class LeakTest
{
	const int N = 30;
	enum Scenario { Control, Leaky, Mitigation }

	[Fact]
	public void VisualStateGroupList_Clear_Leaks()
	{
		var control = Create(Scenario.Control);
		var leaky = Create(Scenario.Leaky);
		var mitigation = Create(Scenario.Mitigation);
		for (var i = 0; i < 7; i++)
		{
			GC.Collect();
			GC.WaitForPendingFinalizers();
			GC.Collect();
		}
		Assert.Equal(0, Alive(control));
		Assert.Equal(N, Alive(leaky));
		Assert.Equal(0, Alive(mitigation));
	}

	[MethodImpl(MethodImplOptions.NoInlining)]
	static List<WeakReference<byte[]>> Create(Scenario scenario)
	{
		var result = new List<WeakReference<byte[]>>();
		var attach = typeof(StateTriggerBase).GetMethod(
			"SendAttached", BindingFlags.Instance | BindingFlags.NonPublic)!;
		var detach = typeof(StateTriggerBase).GetMethod(
			"SendDetached", BindingFlags.Instance | BindingFlags.NonPublic)!;

		for (var i = 0; i < N; i++)
		{
			var payload = new byte[1024 * 1024];
			result.Add(new(payload));
			if (scenario == Scenario.Control)
				continue;

			var trigger = new ProbeTrigger(payload);
			var state = new VisualState { Name = "Probe" };
			state.StateTriggers.Add(trigger);
			var group = new VisualStateGroup();
			group.States.Add(state);
			var groups = new VisualStateGroupList { group };
			VisualStateManager.SetVisualStateGroups(new Label(), groups);
			attach.Invoke(trigger, null);
			if (scenario == Scenario.Mitigation)
				detach.Invoke(trigger, null);
			groups.Clear();
		}
		return result;
	}

	static int Alive(IEnumerable<WeakReference<byte[]>> items)
		=> items.Count(item => item.TryGetTarget(out _));

	sealed class ProbeTrigger(byte[] payload) : StateTriggerBase
	{
		protected override void OnAttached() => Publisher.Changed += Changed;
		protected override void OnDetached() => Publisher.Changed -= Changed;
		void Changed(object? sender, EventArgs e) => GC.KeepAlive(payload);
	}

	static class Publisher
	{
		public static event EventHandler? Changed;
	}
}

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

Observed results

Scenario Alive after full GC Retained payload
Control 0/30 0 MB
Mitigation (SendDetached before Clear) 0/30 0 MB
Leaky (VisualStateGroupList.Clear) 30/30 30 MB

Scope and suggested fix

This affects all platforms when an attached group's trigger subscribes to a long-lived publisher and the group is removed through list mutation. It is a framework lifecycle bug: VisualStateGroupList removal operations should detach all triggers nested in each removed group. Cover Clear, Remove, RemoveAt, replacement by duplicate-name Add, and index replacement.

Generated by Daily Memory Leak Hunter · gpt56 · 707.4 AIC · ⌖ 10.7 AIC · ⊞ 32.5K · ◷

Activity

  1. added
    perf/memory-leak 💦
    on Sep 6, 2026
  2. added
    partner/syncfusion
    on Sep 9, 2026
  3. added a commit that references this issue on Sep 23, 2026
    3164ce8
  4. added this to the .NET 10 SR12 milestone on Sep 23, 2026
  5. added 3 commits that reference this issue on Sep 27, 2026
    450d0ea
    383b218
    dcf8699
  6. added 6 commits that reference this issue on Oct 13, 2026
    736f797
    17c1bcc
    1c6b510
    56f8a6a
    2e52f20
    cce6715
  7. added a commit that references this issue on Oct 28, 2026
    2a3466f
  8. locked and limited conversation to collaborators on Nov 7, 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-workflowspartner/syncfusionperf/memory-leak 💦

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions