Repository navigation
Fix memory leak when replacing IndicatorView.IndicatorTemplate - #39353
Conversation
|
🚀 Dogfood this PR with:
curl -fsSL https://raw.githubusercontent.com/dotnet/maui/main/eng/scripts/get-maui-pr.sh | bash -s -- 39353Or
iex "& { $(irm https://raw.githubusercontent.com/dotnet/maui/main/eng/scripts/get-maui-pr.ps1) } 39353" |
|
Azure Pipelines: Successfully started running 1 pipeline(s). There may be pipelines that require an authorized user to comment /azp run to run. |
There was a problem hiding this comment.
🟡 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
There was a problem hiding this comment.
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.
Expert execution: Verified fork run. Inference and threat detection ran there; this separate trusted job performs native publication.
This comment has been minimized.
This comment has been minimized.
|
Note 🔍
|
PR Test Gate
🧪 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
Execution log excerptsWithout fix With fix PR head: c64dd68b. Without-fix baseline: f16eb798. |
| [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; | ||
| } |
Expert Review
Status: PR closed. No validated expert review was published. 🔎 Expert Analysis — click to expandThe 🧭 Follow-up — actions and refresh
|
<!-- 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"> |
<!-- 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"> |
<!-- 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"> |
<!-- 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"> |
<!-- 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"> |
<!-- 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"> |

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
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.
Before-38578.mov
After-38578.mov