Skip to content

Fix memory leak when replacing IndicatorView.IndicatorTemplate - #39353

Merged
kubaflo merged 4 commits into
dotnet:inflight/currentfrom
devanathan-vaithiyanathan:fix-38578
Oct 14, 2026
Merged

kubaflo merged 4 commits into
dotnet:inflight/currentfrom
devanathan-vaithiyanathan:fix-38578

Conversation

@devanathan-vaithiyanathan

Copy link
Copy Markdown
Contributor

Note

Are you waiting for the changes in this PR to be merged?
It would be very helpful if you could test the resulting artifacts from this PR and let us know in a comment if this change resolves your issue. Thank you!

Issue Details

When an IndicatorView has its IndicatorTemplate replaced with another non-null template, the old internal IndicatorStackLayout remains subscribed to the PropertyChanged event of the IndicatorView.

Root Cause

  • IndicatorStackLayout subscribes to IndicatorView.PropertyChanged during creation.
  • Its Remove() method correctly unsubscribes from the event.
  • However, when IndicatorView.UpdateIndicatorLayout() creates a new layout due to a template change, the old layout is replaced when the new template is non-null.

Description of Change

Replaced direct event subscription to _indicatorView.PropertyChanged with a WeakNotifyPropertyChangedProxy to avoid memory leaks caused by strong event references. The proxy is now used to subscribe and unsubscribe from property changes in IndicatorStackLayout.

Issues Fixed

Fixes #38578

Tested the behavior in the following platforms.

  • Android
  • Windows
  • iOS
  • Mac
Before After
Mac
Before-38578.mov
Mac
After-38578.mov

@github-actions

Copy link
Copy Markdown
Contributor

🚀 Dogfood this PR with:

⚠️ WARNING: Do not do this without first carefully reviewing the code of this PR to satisfy yourself it is safe.

curl -fsSL https://raw.githubusercontent.com/dotnet/maui/main/eng/scripts/get-maui-pr.sh | bash -s -- 39353

Or

  • Run remotely in PowerShell:
iex "& { $(irm https://raw.githubusercontent.com/dotnet/maui/main/eng/scripts/get-maui-pr.ps1) } 39353"

@dotnet-policy-service dotnet-policy-service Bot added the partner/syncfusion Issues / PR's with Syncfusion collaboration label Oct 13, 2026
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).
There may be pipelines that require an authorized user to comment /azp run to run.

@devanathan-vaithiyanathan devanathan-vaithiyanathan added community ✨ Community Contribution partner/syncfusion Issues / PR's with Syncfusion collaboration and removed partner/syncfusion Issues / PR's with Syncfusion collaboration labels Oct 13, 2026
@devanathan-vaithiyanathan devanathan-vaithiyanathan changed the title Fix memory leak when replacing IndicatorView.IndicatorTemplate [WIP]Fix memory leak when replacing IndicatorView.IndicatorTemplate Oct 13, 2026
@devanathan-vaithiyanathan devanathan-vaithiyanathan changed the title [WIP]Fix memory leak when replacing IndicatorView.IndicatorTemplate Fix memory leak when replacing IndicatorView.IndicatorTemplate Oct 13, 2026
@sheiksyedm
sheiksyedm marked this pull request as ready for review October 13, 2026 11:43
Copilot AI balanced review requested due to automatic review settings October 13, 2026 11:43

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The long-lived IndicatorView can still retain stale weak-event proxy instances because finalization does not unsubscribe them.

1 open finding
What changed in this PR

Fixes obsolete IndicatorStackLayout retention when replacing an IndicatorView template.

Changes:

  • Uses a weak property-change subscription.
  • Adds a GC regression test for template replacement.
File Description
IndicatorStackLayout.cs Replaces the strong event subscription with a weak proxy.
IndicatorViewLayoutTests.cs Verifies the previous layout is collectible.

🧠 Review effort: Balanced

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Expert Review - incomplete

AI-generated review using independent GPT dimension passes.

Reviewed commit: c64dd68bf4991997863f98cdaa002fcae89cccbf

Coverage: 3 of 3 routed dimensions completed; finding validation completed.

Findings: 0 new inline findings; 0 review-body findings.

Source/history review only. No builds or tests were executed; this is not merge approval.

Incomplete evidence

  • Historical regression cross-reference failed; do not infer CLEAN from missing evidence.

Workflow run

Expert execution: Verified fork run. Inference and threat detection ran there; this separate trusted job performs native publication.

@kubaflo

This comment has been minimized.

@github-actions github-actions Bot added the s/agent-review-in-progress AI review is currently running for this PR label Oct 13, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Note

🔍 /review gate started

AzDO build 15668452 is running for android.

The s/agent-review-in-progress label stays on this PR while existing PR tests are verified. The final gate report contains test evidence only: no expert review, alternative fixes, or Deep UI sweep runs.

@MauiBot

MauiBot commented Oct 13, 2026

Copy link
Copy Markdown
Collaborator

PR Test Gate

@devanathan-vaithiyanathan — existing-test verification for commit c64dd68b.

Result PASSED Platform Android Commit c64dd68b


🧪 Gate analysis — click to expand

✅ Passed. The selected verification satisfied the gate's passing conditions. Unmatched, skipped, or inconclusive groups are not passing evidence.

🧪 Unit tests
Test group Without fix With fix
IndicatorViewTests ✅ Failed as expected (95s) ✅ Passed (81s)
Execution log excerpts

Without fix IndicatorViewTests

     at Microsoft.Maui.Controls.Core.UnitTests.IndicatorViewTests.ReplacingIndicatorTemplateDoesNotLeakPreviousLayout() in /_/src/Controls/tests/Core.UnitTests/IndicatorViewLayoutTests.cs:line 96

With fix IndicatorViewTests

of 1 test files matched the specified pattern.
[xUnit.net 00:00:00.00] xUnit.net VSTest Adapter v2.8.2+699d445a1a (64-bit .NET 10.0.13)
[xUnit.net 00:00:00.15]   Discovering: Microsoft.Maui.Controls.Core.UnitTests
[xUnit.net 00:00:01.28]   Discovered:  Microsoft.Maui.Controls.Core.UnitTests
[xUnit.net 00:00:01.29]   Starting:    Microsoft.Maui.Controls.Core.UnitTests
  Passed IndicatorLayout_ShouldBeRemovedWhenIndicatorTemplateIsNulled [47 ms]
  Passed IndicatorStackLayoutNoItems_ResetIndicators_ShouldHaveNoChildren [4 ms]
  Passed IndicatorStackLayout_ResetIndicatorCount_ShouldBindChildren(oldCount: 1, expected: 2) [10 ms]
  Passed IndicatorStackLayout_ResetIndicatorCount_ShouldBindChildren(oldCount: -2, expected: 2) [< 1 ms]
  Passed IndicatorStackLayout_ResetIndicatorCount_ShouldBindChildren(oldCount: 0, expected: 2) [< 1 ms]
[xUnit.net 00:00:01.52]   Finished:    Microsoft.Maui.Controls.Core.UnitTests
  Passed ReplacingIndicatorTemplateDoesNotLeakPreviousLayout [35 ms]
  Passed IndicatorViewItemsSourceDoesNotLeak [43 ms]
  Passed IndicatorStackLayoutWithItems_ResetIndicators_ShouldBindChildren [1 ms]
Test Run Successful.
Total tests: 8
     Passed: 8
 Total time: 2.0675 Seconds

PR head: c64dd68b.

Without-fix baseline: f16eb798.

@MauiBot MauiBot added s/agent-gate-passed AI verified tests catch the bug (fail without fix, pass with fix) and removed s/agent-review-in-progress AI review is currently running for this PR labels Oct 13, 2026
Copilot AI balanced review requested due to automatic review settings October 14, 2026 07:34

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The regression test does not verify that the weak proxy itself is detached and collected.

1 open finding
1 resolved since last review

🧠 Review effort: Balanced

Comment on lines +90 to +109
[Fact, Category(TestCategory.Memory)]
public async Task ReplacingIndicatorTemplateDoesNotLeakPreviousLayout()
{
var indicatorView = new IndicatorView();
var weakLayout = ReplaceIndicatorTemplate(indicatorView);

Assert.False(await weakLayout.WaitForCollect(), "The previous IndicatorStackLayout should not be alive!");
GC.KeepAlive(indicatorView);
}

static WeakReference ReplaceIndicatorTemplate(IndicatorView indicatorView)
{
indicatorView.IndicatorTemplate = new DataTemplate();
var oldLayout = Assert.IsType<IndicatorStackLayout>(indicatorView.IndicatorLayout);
var weakLayout = new WeakReference(oldLayout);

indicatorView.IndicatorTemplate = new DataTemplate();

return weakLayout;
}
@kubaflo
kubaflo changed the base branch from main to inflight/current October 14, 2026 10:09
@kubaflo
kubaflo merged commit 4923da1 into dotnet:inflight/current Oct 14, 2026
24 of 33 checks passed
@github-actions github-actions Bot added this to the .NET 10 SR12 milestone Oct 14, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Expert Review

@devanathan-vaithiyanathan — expert-review status for commit cd96b1a.

Scope source review Commit cd96b1a

Status: PR closed. No validated expert review was published.


🔎 Expert Analysis — click to expand

The /review expert-review workflow could not publish a validated review. This is an automation status notice, not a code or test verdict. The PR is now closed; no inline findings were posted.


🧭 Follow-up — actions and refresh

Expert reviews can only be requested on an open PR.

kubaflo pushed a commit that referenced this pull request Oct 14, 2026
<!-- Please let the below note in for people that find this PR -->
> [!NOTE]
> Are you waiting for the changes in this PR to be merged?
> It would be very helpful if you could [test the resulting
artifacts](https://github.com/dotnet/maui/wiki/Testing-PR-Builds) from
this PR and let us know in a comment if this change resolves your issue.
Thank you!

### Issue Details
When an IndicatorView has its IndicatorTemplate replaced with another
non-null template, the old internal IndicatorStackLayout remains
subscribed to the PropertyChanged event of the IndicatorView.

### Root Cause
* IndicatorStackLayout subscribes to IndicatorView.PropertyChanged
during creation.
* Its Remove() method correctly unsubscribes from the event. 
* However, when IndicatorView.UpdateIndicatorLayout() creates a new
layout due to a template change, the old layout is replaced when the new
template is non-null.

### Description of Change

<!-- Enter description of the fix in this section -->
Replaced direct event subscription to _indicatorView.PropertyChanged
with a WeakNotifyPropertyChangedProxy to avoid memory leaks caused by
strong event references. The proxy is now used to subscribe and
unsubscribe from property changes in IndicatorStackLayout.

### Issues Fixed

<!-- Please make sure that there is a bug logged for the issue being
fixed. The bug should describe the problem and how to reproduce it. -->

Fixes #38578 

<!--
Are you targeting main? All PRs should target the main branch unless
otherwise noted.
-->

 **Tested the behavior in the following platforms.**
- [x] Android
- [x] Windows
- [x] iOS
- [x] Mac


| Before  | After  |
|---------|--------|
| **Mac**<br> <video
src="https://github.com/user-attachments/assets/2982e51a-56ba-4efd-b8eb-87f095252aa7"
width="600" height="300"> | **Mac**<br> <video
src="https://github.com/user-attachments/assets/fd7b0b41-1195-495c-b695-93b44ef762b9"
width="600" height="300"> |
kubaflo pushed a commit that referenced this pull request Oct 18, 2026
<!-- Please let the below note in for people that find this PR -->
> [!NOTE]
> Are you waiting for the changes in this PR to be merged?
> It would be very helpful if you could [test the resulting
artifacts](https://github.com/dotnet/maui/wiki/Testing-PR-Builds) from
this PR and let us know in a comment if this change resolves your issue.
Thank you!

### Issue Details
When an IndicatorView has its IndicatorTemplate replaced with another
non-null template, the old internal IndicatorStackLayout remains
subscribed to the PropertyChanged event of the IndicatorView.

### Root Cause
* IndicatorStackLayout subscribes to IndicatorView.PropertyChanged
during creation.
* Its Remove() method correctly unsubscribes from the event. 
* However, when IndicatorView.UpdateIndicatorLayout() creates a new
layout due to a template change, the old layout is replaced when the new
template is non-null.

### Description of Change

<!-- Enter description of the fix in this section -->
Replaced direct event subscription to _indicatorView.PropertyChanged
with a WeakNotifyPropertyChangedProxy to avoid memory leaks caused by
strong event references. The proxy is now used to subscribe and
unsubscribe from property changes in IndicatorStackLayout.

### Issues Fixed

<!-- Please make sure that there is a bug logged for the issue being
fixed. The bug should describe the problem and how to reproduce it. -->

Fixes #38578 

<!--
Are you targeting main? All PRs should target the main branch unless
otherwise noted.
-->

 **Tested the behavior in the following platforms.**
- [x] Android
- [x] Windows
- [x] iOS
- [x] Mac


| Before  | After  |
|---------|--------|
| **Mac**<br> <video
src="https://github.com/user-attachments/assets/2982e51a-56ba-4efd-b8eb-87f095252aa7"
width="600" height="300"> | **Mac**<br> <video
src="https://github.com/user-attachments/assets/fd7b0b41-1195-495c-b695-93b44ef762b9"
width="600" height="300"> |
kubaflo pushed a commit that referenced this pull request Oct 21, 2026
<!-- Please let the below note in for people that find this PR -->
> [!NOTE]
> Are you waiting for the changes in this PR to be merged?
> It would be very helpful if you could [test the resulting
artifacts](https://github.com/dotnet/maui/wiki/Testing-PR-Builds) from
this PR and let us know in a comment if this change resolves your issue.
Thank you!

### Issue Details
When an IndicatorView has its IndicatorTemplate replaced with another
non-null template, the old internal IndicatorStackLayout remains
subscribed to the PropertyChanged event of the IndicatorView.

### Root Cause
* IndicatorStackLayout subscribes to IndicatorView.PropertyChanged
during creation.
* Its Remove() method correctly unsubscribes from the event. 
* However, when IndicatorView.UpdateIndicatorLayout() creates a new
layout due to a template change, the old layout is replaced when the new
template is non-null.

### Description of Change

<!-- Enter description of the fix in this section -->
Replaced direct event subscription to _indicatorView.PropertyChanged
with a WeakNotifyPropertyChangedProxy to avoid memory leaks caused by
strong event references. The proxy is now used to subscribe and
unsubscribe from property changes in IndicatorStackLayout.

### Issues Fixed

<!-- Please make sure that there is a bug logged for the issue being
fixed. The bug should describe the problem and how to reproduce it. -->

Fixes #38578 

<!--
Are you targeting main? All PRs should target the main branch unless
otherwise noted.
-->

 **Tested the behavior in the following platforms.**
- [x] Android
- [x] Windows
- [x] iOS
- [x] Mac


| Before  | After  |
|---------|--------|
| **Mac**<br> <video
src="https://github.com/user-attachments/assets/2982e51a-56ba-4efd-b8eb-87f095252aa7"
width="600" height="300"> | **Mac**<br> <video
src="https://github.com/user-attachments/assets/fd7b0b41-1195-495c-b695-93b44ef762b9"
width="600" height="300"> |
kubaflo pushed a commit that referenced this pull request Oct 25, 2026
<!-- Please let the below note in for people that find this PR -->
> [!NOTE]
> Are you waiting for the changes in this PR to be merged?
> It would be very helpful if you could [test the resulting
artifacts](https://github.com/dotnet/maui/wiki/Testing-PR-Builds) from
this PR and let us know in a comment if this change resolves your issue.
Thank you!

### Issue Details
When an IndicatorView has its IndicatorTemplate replaced with another
non-null template, the old internal IndicatorStackLayout remains
subscribed to the PropertyChanged event of the IndicatorView.

### Root Cause
* IndicatorStackLayout subscribes to IndicatorView.PropertyChanged
during creation.
* Its Remove() method correctly unsubscribes from the event. 
* However, when IndicatorView.UpdateIndicatorLayout() creates a new
layout due to a template change, the old layout is replaced when the new
template is non-null.

### Description of Change

<!-- Enter description of the fix in this section -->
Replaced direct event subscription to _indicatorView.PropertyChanged
with a WeakNotifyPropertyChangedProxy to avoid memory leaks caused by
strong event references. The proxy is now used to subscribe and
unsubscribe from property changes in IndicatorStackLayout.

### Issues Fixed

<!-- Please make sure that there is a bug logged for the issue being
fixed. The bug should describe the problem and how to reproduce it. -->

Fixes #38578 

<!--
Are you targeting main? All PRs should target the main branch unless
otherwise noted.
-->

 **Tested the behavior in the following platforms.**
- [x] Android
- [x] Windows
- [x] iOS
- [x] Mac


| Before  | After  |
|---------|--------|
| **Mac**<br> <video
src="https://github.com/user-attachments/assets/2982e51a-56ba-4efd-b8eb-87f095252aa7"
width="600" height="300"> | **Mac**<br> <video
src="https://github.com/user-attachments/assets/fd7b0b41-1195-495c-b695-93b44ef762b9"
width="600" height="300"> |
kubaflo pushed a commit that referenced this pull request Oct 26, 2026
<!-- Please let the below note in for people that find this PR -->
> [!NOTE]
> Are you waiting for the changes in this PR to be merged?
> It would be very helpful if you could [test the resulting
artifacts](https://github.com/dotnet/maui/wiki/Testing-PR-Builds) from
this PR and let us know in a comment if this change resolves your issue.
Thank you!

### Issue Details
When an IndicatorView has its IndicatorTemplate replaced with another
non-null template, the old internal IndicatorStackLayout remains
subscribed to the PropertyChanged event of the IndicatorView.

### Root Cause
* IndicatorStackLayout subscribes to IndicatorView.PropertyChanged
during creation.
* Its Remove() method correctly unsubscribes from the event. 
* However, when IndicatorView.UpdateIndicatorLayout() creates a new
layout due to a template change, the old layout is replaced when the new
template is non-null.

### Description of Change

<!-- Enter description of the fix in this section -->
Replaced direct event subscription to _indicatorView.PropertyChanged
with a WeakNotifyPropertyChangedProxy to avoid memory leaks caused by
strong event references. The proxy is now used to subscribe and
unsubscribe from property changes in IndicatorStackLayout.

### Issues Fixed

<!-- Please make sure that there is a bug logged for the issue being
fixed. The bug should describe the problem and how to reproduce it. -->

Fixes #38578 

<!--
Are you targeting main? All PRs should target the main branch unless
otherwise noted.
-->

 **Tested the behavior in the following platforms.**
- [x] Android
- [x] Windows
- [x] iOS
- [x] Mac


| Before  | After  |
|---------|--------|
| **Mac**<br> <video
src="https://github.com/user-attachments/assets/2982e51a-56ba-4efd-b8eb-87f095252aa7"
width="600" height="300"> | **Mac**<br> <video
src="https://github.com/user-attachments/assets/fd7b0b41-1195-495c-b695-93b44ef762b9"
width="600" height="300"> |
github-actions Bot pushed a commit that referenced this pull request Oct 28, 2026
<!-- Please let the below note in for people that find this PR -->
> [!NOTE]
> Are you waiting for the changes in this PR to be merged?
> It would be very helpful if you could [test the resulting
artifacts](https://github.com/dotnet/maui/wiki/Testing-PR-Builds) from
this PR and let us know in a comment if this change resolves your issue.
Thank you!

### Issue Details
When an IndicatorView has its IndicatorTemplate replaced with another
non-null template, the old internal IndicatorStackLayout remains
subscribed to the PropertyChanged event of the IndicatorView.

### Root Cause
* IndicatorStackLayout subscribes to IndicatorView.PropertyChanged
during creation.
* Its Remove() method correctly unsubscribes from the event. 
* However, when IndicatorView.UpdateIndicatorLayout() creates a new
layout due to a template change, the old layout is replaced when the new
template is non-null.

### Description of Change

<!-- Enter description of the fix in this section -->
Replaced direct event subscription to _indicatorView.PropertyChanged
with a WeakNotifyPropertyChangedProxy to avoid memory leaks caused by
strong event references. The proxy is now used to subscribe and
unsubscribe from property changes in IndicatorStackLayout.

### Issues Fixed

<!-- Please make sure that there is a bug logged for the issue being
fixed. The bug should describe the problem and how to reproduce it. -->

Fixes #38578 

<!--
Are you targeting main? All PRs should target the main branch unless
otherwise noted.
-->

 **Tested the behavior in the following platforms.**
- [x] Android
- [x] Windows
- [x] iOS
- [x] Mac


| Before  | After  |
|---------|--------|
| **Mac**<br> <video
src="https://github.com/user-attachments/assets/2982e51a-56ba-4efd-b8eb-87f095252aa7"
width="600" height="300"> | **Mac**<br> <video
src="https://github.com/user-attachments/assets/fd7b0b41-1195-495c-b695-93b44ef762b9"
width="600" height="300"> |
@github-actions github-actions Bot locked and limited conversation to collaborators Nov 14, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

community ✨ Community Contribution partner/syncfusion Issues / PR's with Syncfusion collaboration s/agent-gate-passed AI verified tests catch the bug (fail without fix, pass with fix)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[leak-scan] IndicatorView.IndicatorTemplate — replacement leaves obsolete layout subscribed

4 participants