Repository navigation
Fix GradientBrush.GradientStops.Clear() leaks brushes when stops are shared - #36746
Merged
kubaflo merged 6 commits intoJul 26, 2026
Merged
Conversation
devanathan-vaithiyanathan
had a problem deploying
to
copilot-pat-pool
July 23, 2026 10:30 — with
GitHub Actions
Failure
Contributor
|
🚀 Dogfood this PR with:
curl -fsSL https://raw.githubusercontent.com/dotnet/maui/main/eng/scripts/get-maui-pr.sh | bash -s -- 36746Or
iex "& { $(irm https://raw.githubusercontent.com/dotnet/maui/main/eng/scripts/get-maui-pr.ps1) } 36746" |
|
Azure Pipelines: Successfully started running 1 pipeline(s). There may be pipelines that require an authorized user to comment /azp run to run. |
sheiksyedm
marked this pull request as ready for review
July 23, 2026 15:11
|
Azure Pipelines: Successfully started running 1 pipeline(s). There may be pipelines that require an authorized user to comment /azp run to run. |
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes a memory leak in GradientBrush where GradientStops.Clear() (Reset notifications with OldItems == null) could leave GradientStop.PropertyChanged handlers subscribed, keeping brushes alive when stops are shared.
Changes:
- Refactors
GradientBrush’s collection attach/detach logic and expandsCollectionChangedhandling to cover all actions, includingReset. - Introduces subscription tracking with reference counting to correctly subscribe/unsubscribe stops (including duplicate instances in the collection).
- Adds a unit test ensuring
Clear()unsubscribes previous gradient stops and prevents spurious invalidations.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| src/Controls/src/Core/GradientBrush.cs | Adds ref-counted subscription tracking and robust NotifyCollectionChangedAction handling, including Reset resubscription. |
| src/Controls/tests/Core.UnitTests/LinearGradientBrushTests.cs | Adds a regression test validating Clear() unsubscribes old stops from PropertyChanged. |
Open
3 tasks
This comment has been minimized.
This comment has been minimized.
MauiBot
reviewed
Jul 24, 2026
MauiBot
left a comment
Collaborator
There was a problem hiding this comment.
Expert Review — 2 findings
See inline comments for details.
kubaflo
pushed a commit
that referenced
this pull request
Aug 12, 2026
…shared (#36746) <!-- 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 GradientBrush subscribes to each GradientStop's PropertyChanged event, but GradientStops.Clear() raises a collection Reset with e.OldItems == null. Because GradientBrush.OnGradientStopCollectionChanged only unsubscribes stops from e.OldItems, cleared stops remain subscribed. ### Description of Change <!-- Enter description of the fix in this section --> Added a dictionary to track GradientStop subscriptions and manage event subscriptions correctly. Updated the collection change handling to support all collection actions, including Add, Remove, Replace, Move, and Reset. Fixed the Reset scenario by properly unsubscribing from existing GradientStop instances before re-subscribing to the current collection items. Also added reference counting to correctly handle duplicate GradientStop instances and refactored the collection attach/detach logic into helper methods to improve code maintainability. ### 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 #36743 <!-- 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 | |---------|--------| | **Android**<br> <video src="https://github.com/user-attachments/assets/6c924225-c68a-44e6-8062-e705eaeb8402" width="600" height="300"> | **Android**<br> <video src="https://github.com/user-attachments/assets/28315b3b-05a6-4935-8f86-0316058986a4" width="600" height="300"> |
kubaflo
added a commit
that referenced
this pull request
Aug 12, 2026
…GradientBrush (#36808) > [!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! ### Problem `GradientBrush` (added in #36746) keys its subscription ref-count dictionary with `System.Collections.Generic.ReferenceEqualityComparer.Instance`, which is **.NET 5+ only**. `Controls.Core` also targets **netstandard2.0/2.1**, so the shared build breaks: ``` src/Controls/src/Core/GradientBrush.cs(13,71): error CS0122: 'ReferenceEqualityComparer' is inaccessible due to its protection level [Controls.Core.csproj::TargetFramework=netstandard2.0] ``` This blocks every build of `inflight/current` (including Copilot PR reviews on inflight PRs, e.g. #36776, where the gate could not build the base). ### Fix Replace the BCL comparer with a small self-contained reference-identity comparer that works on **every** TFM — equality by object reference, hash from `RuntimeHelpers.GetHashCode` — preserving the leak fix's exact semantics. The other `ReferenceEqualityComparer` uses (`ShellRenderer.cs`, `MapHandler.Android.cs`) are in platform-specific files that only compile for .NET TFMs, so they are unaffected. ### Risk Minimal — behaviorally identical to `ReferenceEqualityComparer.Instance`; no public API change. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 15d2af20-e4ab-4e88-9011-cfbd83513bc0
kubaflo
pushed a commit
that referenced
this pull request
Aug 27, 2026
…shared (#36746) <!-- 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 GradientBrush subscribes to each GradientStop's PropertyChanged event, but GradientStops.Clear() raises a collection Reset with e.OldItems == null. Because GradientBrush.OnGradientStopCollectionChanged only unsubscribes stops from e.OldItems, cleared stops remain subscribed. ### Description of Change <!-- Enter description of the fix in this section --> Added a dictionary to track GradientStop subscriptions and manage event subscriptions correctly. Updated the collection change handling to support all collection actions, including Add, Remove, Replace, Move, and Reset. Fixed the Reset scenario by properly unsubscribing from existing GradientStop instances before re-subscribing to the current collection items. Also added reference counting to correctly handle duplicate GradientStop instances and refactored the collection attach/detach logic into helper methods to improve code maintainability. ### 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 #36743 <!-- 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 | |---------|--------| | **Android**<br> <video src="https://github.com/user-attachments/assets/6c924225-c68a-44e6-8062-e705eaeb8402" width="600" height="300"> | **Android**<br> <video src="https://github.com/user-attachments/assets/28315b3b-05a6-4935-8f86-0316058986a4" width="600" height="300"> |
kubaflo
added a commit
that referenced
this pull request
Aug 27, 2026
…GradientBrush (#36808) > [!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! ### Problem `GradientBrush` (added in #36746) keys its subscription ref-count dictionary with `System.Collections.Generic.ReferenceEqualityComparer.Instance`, which is **.NET 5+ only**. `Controls.Core` also targets **netstandard2.0/2.1**, so the shared build breaks: ``` src/Controls/src/Core/GradientBrush.cs(13,71): error CS0122: 'ReferenceEqualityComparer' is inaccessible due to its protection level [Controls.Core.csproj::TargetFramework=netstandard2.0] ``` This blocks every build of `inflight/current` (including Copilot PR reviews on inflight PRs, e.g. #36776, where the gate could not build the base). ### Fix Replace the BCL comparer with a small self-contained reference-identity comparer that works on **every** TFM — equality by object reference, hash from `RuntimeHelpers.GetHashCode` — preserving the leak fix's exact semantics. The other `ReferenceEqualityComparer` uses (`ShellRenderer.cs`, `MapHandler.Android.cs`) are in platform-specific files that only compile for .NET TFMs, so they are unaffected. ### Risk Minimal — behaviorally identical to `ReferenceEqualityComparer.Instance`; no public API change. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 15d2af20-e4ab-4e88-9011-cfbd83513bc0
kubaflo
pushed a commit
that referenced
this pull request
Sep 2, 2026
…shared (#36746) <!-- 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 GradientBrush subscribes to each GradientStop's PropertyChanged event, but GradientStops.Clear() raises a collection Reset with e.OldItems == null. Because GradientBrush.OnGradientStopCollectionChanged only unsubscribes stops from e.OldItems, cleared stops remain subscribed. ### Description of Change <!-- Enter description of the fix in this section --> Added a dictionary to track GradientStop subscriptions and manage event subscriptions correctly. Updated the collection change handling to support all collection actions, including Add, Remove, Replace, Move, and Reset. Fixed the Reset scenario by properly unsubscribing from existing GradientStop instances before re-subscribing to the current collection items. Also added reference counting to correctly handle duplicate GradientStop instances and refactored the collection attach/detach logic into helper methods to improve code maintainability. ### 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 #36743 <!-- 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 | |---------|--------| | **Android**<br> <video src="https://github.com/user-attachments/assets/6c924225-c68a-44e6-8062-e705eaeb8402" width="600" height="300"> | **Android**<br> <video src="https://github.com/user-attachments/assets/28315b3b-05a6-4935-8f86-0316058986a4" width="600" height="300"> |
kubaflo
added a commit
that referenced
this pull request
Sep 2, 2026
…GradientBrush (#36808) > [!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! ### Problem `GradientBrush` (added in #36746) keys its subscription ref-count dictionary with `System.Collections.Generic.ReferenceEqualityComparer.Instance`, which is **.NET 5+ only**. `Controls.Core` also targets **netstandard2.0/2.1**, so the shared build breaks: ``` src/Controls/src/Core/GradientBrush.cs(13,71): error CS0122: 'ReferenceEqualityComparer' is inaccessible due to its protection level [Controls.Core.csproj::TargetFramework=netstandard2.0] ``` This blocks every build of `inflight/current` (including Copilot PR reviews on inflight PRs, e.g. #36776, where the gate could not build the base). ### Fix Replace the BCL comparer with a small self-contained reference-identity comparer that works on **every** TFM — equality by object reference, hash from `RuntimeHelpers.GetHashCode` — preserving the leak fix's exact semantics. The other `ReferenceEqualityComparer` uses (`ShellRenderer.cs`, `MapHandler.Android.cs`) are in platform-specific files that only compile for .NET TFMs, so they are unaffected. ### Risk Minimal — behaviorally identical to `ReferenceEqualityComparer.Instance`; no public API change. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 15d2af20-e4ab-4e88-9011-cfbd83513bc0
kubaflo
pushed a commit
that referenced
this pull request
Sep 3, 2026
…shared (#36746) <!-- 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 GradientBrush subscribes to each GradientStop's PropertyChanged event, but GradientStops.Clear() raises a collection Reset with e.OldItems == null. Because GradientBrush.OnGradientStopCollectionChanged only unsubscribes stops from e.OldItems, cleared stops remain subscribed. ### Description of Change <!-- Enter description of the fix in this section --> Added a dictionary to track GradientStop subscriptions and manage event subscriptions correctly. Updated the collection change handling to support all collection actions, including Add, Remove, Replace, Move, and Reset. Fixed the Reset scenario by properly unsubscribing from existing GradientStop instances before re-subscribing to the current collection items. Also added reference counting to correctly handle duplicate GradientStop instances and refactored the collection attach/detach logic into helper methods to improve code maintainability. ### 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 #36743 <!-- 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 | |---------|--------| | **Android**<br> <video src="https://github.com/user-attachments/assets/6c924225-c68a-44e6-8062-e705eaeb8402" width="600" height="300"> | **Android**<br> <video src="https://github.com/user-attachments/assets/28315b3b-05a6-4935-8f86-0316058986a4" width="600" height="300"> |
kubaflo
added a commit
that referenced
this pull request
Sep 3, 2026
…GradientBrush (#36808) > [!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! ### Problem `GradientBrush` (added in #36746) keys its subscription ref-count dictionary with `System.Collections.Generic.ReferenceEqualityComparer.Instance`, which is **.NET 5+ only**. `Controls.Core` also targets **netstandard2.0/2.1**, so the shared build breaks: ``` src/Controls/src/Core/GradientBrush.cs(13,71): error CS0122: 'ReferenceEqualityComparer' is inaccessible due to its protection level [Controls.Core.csproj::TargetFramework=netstandard2.0] ``` This blocks every build of `inflight/current` (including Copilot PR reviews on inflight PRs, e.g. #36776, where the gate could not build the base). ### Fix Replace the BCL comparer with a small self-contained reference-identity comparer that works on **every** TFM — equality by object reference, hash from `RuntimeHelpers.GetHashCode` — preserving the leak fix's exact semantics. The other `ReferenceEqualityComparer` uses (`ShellRenderer.cs`, `MapHandler.Android.cs`) are in platform-specific files that only compile for .NET TFMs, so they are unaffected. ### Risk Minimal — behaviorally identical to `ReferenceEqualityComparer.Instance`; no public API change. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 15d2af20-e4ab-4e88-9011-cfbd83513bc0
kubaflo
pushed a commit
that referenced
this pull request
Sep 3, 2026
…shared (#36746) <!-- 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 GradientBrush subscribes to each GradientStop's PropertyChanged event, but GradientStops.Clear() raises a collection Reset with e.OldItems == null. Because GradientBrush.OnGradientStopCollectionChanged only unsubscribes stops from e.OldItems, cleared stops remain subscribed. ### Description of Change <!-- Enter description of the fix in this section --> Added a dictionary to track GradientStop subscriptions and manage event subscriptions correctly. Updated the collection change handling to support all collection actions, including Add, Remove, Replace, Move, and Reset. Fixed the Reset scenario by properly unsubscribing from existing GradientStop instances before re-subscribing to the current collection items. Also added reference counting to correctly handle duplicate GradientStop instances and refactored the collection attach/detach logic into helper methods to improve code maintainability. ### 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 #36743 <!-- 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 | |---------|--------| | **Android**<br> <video src="https://github.com/user-attachments/assets/6c924225-c68a-44e6-8062-e705eaeb8402" width="600" height="300"> | **Android**<br> <video src="https://github.com/user-attachments/assets/28315b3b-05a6-4935-8f86-0316058986a4" width="600" height="300"> |
kubaflo
added a commit
that referenced
this pull request
Sep 3, 2026
…GradientBrush (#36808) > [!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! ### Problem `GradientBrush` (added in #36746) keys its subscription ref-count dictionary with `System.Collections.Generic.ReferenceEqualityComparer.Instance`, which is **.NET 5+ only**. `Controls.Core` also targets **netstandard2.0/2.1**, so the shared build breaks: ``` src/Controls/src/Core/GradientBrush.cs(13,71): error CS0122: 'ReferenceEqualityComparer' is inaccessible due to its protection level [Controls.Core.csproj::TargetFramework=netstandard2.0] ``` This blocks every build of `inflight/current` (including Copilot PR reviews on inflight PRs, e.g. #36776, where the gate could not build the base). ### Fix Replace the BCL comparer with a small self-contained reference-identity comparer that works on **every** TFM — equality by object reference, hash from `RuntimeHelpers.GetHashCode` — preserving the leak fix's exact semantics. The other `ReferenceEqualityComparer` uses (`ShellRenderer.cs`, `MapHandler.Android.cs`) are in platform-specific files that only compile for .NET TFMs, so they are unaffected. ### Risk Minimal — behaviorally identical to `ReferenceEqualityComparer.Instance`; no public API change. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 15d2af20-e4ab-4e88-9011-cfbd83513bc0
kubaflo
pushed a commit
that referenced
this pull request
Sep 4, 2026
…shared (#36746) <!-- 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 GradientBrush subscribes to each GradientStop's PropertyChanged event, but GradientStops.Clear() raises a collection Reset with e.OldItems == null. Because GradientBrush.OnGradientStopCollectionChanged only unsubscribes stops from e.OldItems, cleared stops remain subscribed. ### Description of Change <!-- Enter description of the fix in this section --> Added a dictionary to track GradientStop subscriptions and manage event subscriptions correctly. Updated the collection change handling to support all collection actions, including Add, Remove, Replace, Move, and Reset. Fixed the Reset scenario by properly unsubscribing from existing GradientStop instances before re-subscribing to the current collection items. Also added reference counting to correctly handle duplicate GradientStop instances and refactored the collection attach/detach logic into helper methods to improve code maintainability. ### 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 #36743 <!-- 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 | |---------|--------| | **Android**<br> <video src="https://github.com/user-attachments/assets/6c924225-c68a-44e6-8062-e705eaeb8402" width="600" height="300"> | **Android**<br> <video src="https://github.com/user-attachments/assets/28315b3b-05a6-4935-8f86-0316058986a4" width="600" height="300"> |
kubaflo
added a commit
that referenced
this pull request
Sep 4, 2026
…GradientBrush (#36808) > [!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! ### Problem `GradientBrush` (added in #36746) keys its subscription ref-count dictionary with `System.Collections.Generic.ReferenceEqualityComparer.Instance`, which is **.NET 5+ only**. `Controls.Core` also targets **netstandard2.0/2.1**, so the shared build breaks: ``` src/Controls/src/Core/GradientBrush.cs(13,71): error CS0122: 'ReferenceEqualityComparer' is inaccessible due to its protection level [Controls.Core.csproj::TargetFramework=netstandard2.0] ``` This blocks every build of `inflight/current` (including Copilot PR reviews on inflight PRs, e.g. #36776, where the gate could not build the base). ### Fix Replace the BCL comparer with a small self-contained reference-identity comparer that works on **every** TFM — equality by object reference, hash from `RuntimeHelpers.GetHashCode` — preserving the leak fix's exact semantics. The other `ReferenceEqualityComparer` uses (`ShellRenderer.cs`, `MapHandler.Android.cs`) are in platform-specific files that only compile for .NET TFMs, so they are unaffected. ### Risk Minimal — behaviorally identical to `ReferenceEqualityComparer.Instance`; no public API change. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 15d2af20-e4ab-4e88-9011-cfbd83513bc0
kubaflo
pushed a commit
that referenced
this pull request
Sep 6, 2026
…shared (#36746) <!-- 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 GradientBrush subscribes to each GradientStop's PropertyChanged event, but GradientStops.Clear() raises a collection Reset with e.OldItems == null. Because GradientBrush.OnGradientStopCollectionChanged only unsubscribes stops from e.OldItems, cleared stops remain subscribed. ### Description of Change <!-- Enter description of the fix in this section --> Added a dictionary to track GradientStop subscriptions and manage event subscriptions correctly. Updated the collection change handling to support all collection actions, including Add, Remove, Replace, Move, and Reset. Fixed the Reset scenario by properly unsubscribing from existing GradientStop instances before re-subscribing to the current collection items. Also added reference counting to correctly handle duplicate GradientStop instances and refactored the collection attach/detach logic into helper methods to improve code maintainability. ### 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 #36743 <!-- 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 | |---------|--------| | **Android**<br> <video src="https://github.com/user-attachments/assets/6c924225-c68a-44e6-8062-e705eaeb8402" width="600" height="300"> | **Android**<br> <video src="https://github.com/user-attachments/assets/28315b3b-05a6-4935-8f86-0316058986a4" width="600" height="300"> |
kubaflo
added a commit
that referenced
this pull request
Sep 6, 2026
…GradientBrush (#36808) > [!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! ### Problem `GradientBrush` (added in #36746) keys its subscription ref-count dictionary with `System.Collections.Generic.ReferenceEqualityComparer.Instance`, which is **.NET 5+ only**. `Controls.Core` also targets **netstandard2.0/2.1**, so the shared build breaks: ``` src/Controls/src/Core/GradientBrush.cs(13,71): error CS0122: 'ReferenceEqualityComparer' is inaccessible due to its protection level [Controls.Core.csproj::TargetFramework=netstandard2.0] ``` This blocks every build of `inflight/current` (including Copilot PR reviews on inflight PRs, e.g. #36776, where the gate could not build the base). ### Fix Replace the BCL comparer with a small self-contained reference-identity comparer that works on **every** TFM — equality by object reference, hash from `RuntimeHelpers.GetHashCode` — preserving the leak fix's exact semantics. The other `ReferenceEqualityComparer` uses (`ShellRenderer.cs`, `MapHandler.Android.cs`) are in platform-specific files that only compile for .NET TFMs, so they are unaffected. ### Risk Minimal — behaviorally identical to `ReferenceEqualityComparer.Instance`; no public API change. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 15d2af20-e4ab-4e88-9011-cfbd83513bc0
kubaflo
pushed a commit
that referenced
this pull request
Sep 7, 2026
…shared (#36746) <!-- 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 GradientBrush subscribes to each GradientStop's PropertyChanged event, but GradientStops.Clear() raises a collection Reset with e.OldItems == null. Because GradientBrush.OnGradientStopCollectionChanged only unsubscribes stops from e.OldItems, cleared stops remain subscribed. ### Description of Change <!-- Enter description of the fix in this section --> Added a dictionary to track GradientStop subscriptions and manage event subscriptions correctly. Updated the collection change handling to support all collection actions, including Add, Remove, Replace, Move, and Reset. Fixed the Reset scenario by properly unsubscribing from existing GradientStop instances before re-subscribing to the current collection items. Also added reference counting to correctly handle duplicate GradientStop instances and refactored the collection attach/detach logic into helper methods to improve code maintainability. ### 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 #36743 <!-- 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 | |---------|--------| | **Android**<br> <video src="https://github.com/user-attachments/assets/6c924225-c68a-44e6-8062-e705eaeb8402" width="600" height="300"> | **Android**<br> <video src="https://github.com/user-attachments/assets/28315b3b-05a6-4935-8f86-0316058986a4" width="600" height="300"> |
kubaflo
added a commit
that referenced
this pull request
Sep 7, 2026
…GradientBrush (#36808) > [!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! ### Problem `GradientBrush` (added in #36746) keys its subscription ref-count dictionary with `System.Collections.Generic.ReferenceEqualityComparer.Instance`, which is **.NET 5+ only**. `Controls.Core` also targets **netstandard2.0/2.1**, so the shared build breaks: ``` src/Controls/src/Core/GradientBrush.cs(13,71): error CS0122: 'ReferenceEqualityComparer' is inaccessible due to its protection level [Controls.Core.csproj::TargetFramework=netstandard2.0] ``` This blocks every build of `inflight/current` (including Copilot PR reviews on inflight PRs, e.g. #36776, where the gate could not build the base). ### Fix Replace the BCL comparer with a small self-contained reference-identity comparer that works on **every** TFM — equality by object reference, hash from `RuntimeHelpers.GetHashCode` — preserving the leak fix's exact semantics. The other `ReferenceEqualityComparer` uses (`ShellRenderer.cs`, `MapHandler.Android.cs`) are in platform-specific files that only compile for .NET TFMs, so they are unaffected. ### Risk Minimal — behaviorally identical to `ReferenceEqualityComparer.Instance`; no public API change. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 15d2af20-e4ab-4e88-9011-cfbd83513bc0
kubaflo
pushed a commit
that referenced
this pull request
Sep 7, 2026
…shared (#36746) <!-- 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 GradientBrush subscribes to each GradientStop's PropertyChanged event, but GradientStops.Clear() raises a collection Reset with e.OldItems == null. Because GradientBrush.OnGradientStopCollectionChanged only unsubscribes stops from e.OldItems, cleared stops remain subscribed. ### Description of Change <!-- Enter description of the fix in this section --> Added a dictionary to track GradientStop subscriptions and manage event subscriptions correctly. Updated the collection change handling to support all collection actions, including Add, Remove, Replace, Move, and Reset. Fixed the Reset scenario by properly unsubscribing from existing GradientStop instances before re-subscribing to the current collection items. Also added reference counting to correctly handle duplicate GradientStop instances and refactored the collection attach/detach logic into helper methods to improve code maintainability. ### 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 #36743 <!-- 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 | |---------|--------| | **Android**<br> <video src="https://github.com/user-attachments/assets/6c924225-c68a-44e6-8062-e705eaeb8402" width="600" height="300"> | **Android**<br> <video src="https://github.com/user-attachments/assets/28315b3b-05a6-4935-8f86-0316058986a4" width="600" height="300"> |
kubaflo
added a commit
that referenced
this pull request
Sep 7, 2026
…GradientBrush (#36808) > [!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! ### Problem `GradientBrush` (added in #36746) keys its subscription ref-count dictionary with `System.Collections.Generic.ReferenceEqualityComparer.Instance`, which is **.NET 5+ only**. `Controls.Core` also targets **netstandard2.0/2.1**, so the shared build breaks: ``` src/Controls/src/Core/GradientBrush.cs(13,71): error CS0122: 'ReferenceEqualityComparer' is inaccessible due to its protection level [Controls.Core.csproj::TargetFramework=netstandard2.0] ``` This blocks every build of `inflight/current` (including Copilot PR reviews on inflight PRs, e.g. #36776, where the gate could not build the base). ### Fix Replace the BCL comparer with a small self-contained reference-identity comparer that works on **every** TFM — equality by object reference, hash from `RuntimeHelpers.GetHashCode` — preserving the leak fix's exact semantics. The other `ReferenceEqualityComparer` uses (`ShellRenderer.cs`, `MapHandler.Android.cs`) are in platform-specific files that only compile for .NET TFMs, so they are unaffected. ### Risk Minimal — behaviorally identical to `ReferenceEqualityComparer.Instance`; no public API change. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 15d2af20-e4ab-4e88-9011-cfbd83513bc0
kubaflo
pushed a commit
that referenced
this pull request
Sep 8, 2026
…shared (#36746) <!-- 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 GradientBrush subscribes to each GradientStop's PropertyChanged event, but GradientStops.Clear() raises a collection Reset with e.OldItems == null. Because GradientBrush.OnGradientStopCollectionChanged only unsubscribes stops from e.OldItems, cleared stops remain subscribed. ### Description of Change <!-- Enter description of the fix in this section --> Added a dictionary to track GradientStop subscriptions and manage event subscriptions correctly. Updated the collection change handling to support all collection actions, including Add, Remove, Replace, Move, and Reset. Fixed the Reset scenario by properly unsubscribing from existing GradientStop instances before re-subscribing to the current collection items. Also added reference counting to correctly handle duplicate GradientStop instances and refactored the collection attach/detach logic into helper methods to improve code maintainability. ### 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 #36743 <!-- 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 | |---------|--------| | **Android**<br> <video src="https://github.com/user-attachments/assets/6c924225-c68a-44e6-8062-e705eaeb8402" width="600" height="300"> | **Android**<br> <video src="https://github.com/user-attachments/assets/28315b3b-05a6-4935-8f86-0316058986a4" width="600" height="300"> |
kubaflo
added a commit
that referenced
this pull request
Sep 8, 2026
…GradientBrush (#36808) > [!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! ### Problem `GradientBrush` (added in #36746) keys its subscription ref-count dictionary with `System.Collections.Generic.ReferenceEqualityComparer.Instance`, which is **.NET 5+ only**. `Controls.Core` also targets **netstandard2.0/2.1**, so the shared build breaks: ``` src/Controls/src/Core/GradientBrush.cs(13,71): error CS0122: 'ReferenceEqualityComparer' is inaccessible due to its protection level [Controls.Core.csproj::TargetFramework=netstandard2.0] ``` This blocks every build of `inflight/current` (including Copilot PR reviews on inflight PRs, e.g. #36776, where the gate could not build the base). ### Fix Replace the BCL comparer with a small self-contained reference-identity comparer that works on **every** TFM — equality by object reference, hash from `RuntimeHelpers.GetHashCode` — preserving the leak fix's exact semantics. The other `ReferenceEqualityComparer` uses (`ShellRenderer.cs`, `MapHandler.Android.cs`) are in platform-specific files that only compile for .NET TFMs, so they are unaffected. ### Risk Minimal — behaviorally identical to `ReferenceEqualityComparer.Instance`; no public API change. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 15d2af20-e4ab-4e88-9011-cfbd83513bc0
kubaflo
pushed a commit
that referenced
this pull request
Sep 8, 2026
…shared (#36746) <!-- 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 GradientBrush subscribes to each GradientStop's PropertyChanged event, but GradientStops.Clear() raises a collection Reset with e.OldItems == null. Because GradientBrush.OnGradientStopCollectionChanged only unsubscribes stops from e.OldItems, cleared stops remain subscribed. ### Description of Change <!-- Enter description of the fix in this section --> Added a dictionary to track GradientStop subscriptions and manage event subscriptions correctly. Updated the collection change handling to support all collection actions, including Add, Remove, Replace, Move, and Reset. Fixed the Reset scenario by properly unsubscribing from existing GradientStop instances before re-subscribing to the current collection items. Also added reference counting to correctly handle duplicate GradientStop instances and refactored the collection attach/detach logic into helper methods to improve code maintainability. ### 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 #36743 <!-- 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 | |---------|--------| | **Android**<br> <video src="https://github.com/user-attachments/assets/6c924225-c68a-44e6-8062-e705eaeb8402" width="600" height="300"> | **Android**<br> <video src="https://github.com/user-attachments/assets/28315b3b-05a6-4935-8f86-0316058986a4" width="600" height="300"> |
kubaflo
added a commit
that referenced
this pull request
Sep 8, 2026
…GradientBrush (#36808) > [!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! ### Problem `GradientBrush` (added in #36746) keys its subscription ref-count dictionary with `System.Collections.Generic.ReferenceEqualityComparer.Instance`, which is **.NET 5+ only**. `Controls.Core` also targets **netstandard2.0/2.1**, so the shared build breaks: ``` src/Controls/src/Core/GradientBrush.cs(13,71): error CS0122: 'ReferenceEqualityComparer' is inaccessible due to its protection level [Controls.Core.csproj::TargetFramework=netstandard2.0] ``` This blocks every build of `inflight/current` (including Copilot PR reviews on inflight PRs, e.g. #36776, where the gate could not build the base). ### Fix Replace the BCL comparer with a small self-contained reference-identity comparer that works on **every** TFM — equality by object reference, hash from `RuntimeHelpers.GetHashCode` — preserving the leak fix's exact semantics. The other `ReferenceEqualityComparer` uses (`ShellRenderer.cs`, `MapHandler.Android.cs`) are in platform-specific files that only compile for .NET TFMs, so they are unaffected. ### Risk Minimal — behaviorally identical to `ReferenceEqualityComparer.Instance`; no public API change. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 15d2af20-e4ab-4e88-9011-cfbd83513bc0
kubaflo
pushed a commit
that referenced
this pull request
Sep 9, 2026
…shared (#36746) <!-- 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 GradientBrush subscribes to each GradientStop's PropertyChanged event, but GradientStops.Clear() raises a collection Reset with e.OldItems == null. Because GradientBrush.OnGradientStopCollectionChanged only unsubscribes stops from e.OldItems, cleared stops remain subscribed. ### Description of Change <!-- Enter description of the fix in this section --> Added a dictionary to track GradientStop subscriptions and manage event subscriptions correctly. Updated the collection change handling to support all collection actions, including Add, Remove, Replace, Move, and Reset. Fixed the Reset scenario by properly unsubscribing from existing GradientStop instances before re-subscribing to the current collection items. Also added reference counting to correctly handle duplicate GradientStop instances and refactored the collection attach/detach logic into helper methods to improve code maintainability. ### 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 #36743 <!-- 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 | |---------|--------| | **Android**<br> <video src="https://github.com/user-attachments/assets/6c924225-c68a-44e6-8062-e705eaeb8402" width="600" height="300"> | **Android**<br> <video src="https://github.com/user-attachments/assets/28315b3b-05a6-4935-8f86-0316058986a4" width="600" height="300"> |
kubaflo
added a commit
that referenced
this pull request
Sep 9, 2026
…GradientBrush (#36808) > [!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! ### Problem `GradientBrush` (added in #36746) keys its subscription ref-count dictionary with `System.Collections.Generic.ReferenceEqualityComparer.Instance`, which is **.NET 5+ only**. `Controls.Core` also targets **netstandard2.0/2.1**, so the shared build breaks: ``` src/Controls/src/Core/GradientBrush.cs(13,71): error CS0122: 'ReferenceEqualityComparer' is inaccessible due to its protection level [Controls.Core.csproj::TargetFramework=netstandard2.0] ``` This blocks every build of `inflight/current` (including Copilot PR reviews on inflight PRs, e.g. #36776, where the gate could not build the base). ### Fix Replace the BCL comparer with a small self-contained reference-identity comparer that works on **every** TFM — equality by object reference, hash from `RuntimeHelpers.GetHashCode` — preserving the leak fix's exact semantics. The other `ReferenceEqualityComparer` uses (`ShellRenderer.cs`, `MapHandler.Android.cs`) are in platform-specific files that only compile for .NET TFMs, so they are unaffected. ### Risk Minimal — behaviorally identical to `ReferenceEqualityComparer.Instance`; no public API change. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 15d2af20-e4ab-4e88-9011-cfbd83513bc0
kubaflo
pushed a commit
that referenced
this pull request
Sep 9, 2026
…shared (#36746) <!-- 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 GradientBrush subscribes to each GradientStop's PropertyChanged event, but GradientStops.Clear() raises a collection Reset with e.OldItems == null. Because GradientBrush.OnGradientStopCollectionChanged only unsubscribes stops from e.OldItems, cleared stops remain subscribed. ### Description of Change <!-- Enter description of the fix in this section --> Added a dictionary to track GradientStop subscriptions and manage event subscriptions correctly. Updated the collection change handling to support all collection actions, including Add, Remove, Replace, Move, and Reset. Fixed the Reset scenario by properly unsubscribing from existing GradientStop instances before re-subscribing to the current collection items. Also added reference counting to correctly handle duplicate GradientStop instances and refactored the collection attach/detach logic into helper methods to improve code maintainability. ### 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 #36743 <!-- 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 | |---------|--------| | **Android**<br> <video src="https://github.com/user-attachments/assets/6c924225-c68a-44e6-8062-e705eaeb8402" width="600" height="300"> | **Android**<br> <video src="https://github.com/user-attachments/assets/28315b3b-05a6-4935-8f86-0316058986a4" width="600" height="300"> |
kubaflo
added a commit
that referenced
this pull request
Sep 9, 2026
…GradientBrush (#36808) > [!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! ### Problem `GradientBrush` (added in #36746) keys its subscription ref-count dictionary with `System.Collections.Generic.ReferenceEqualityComparer.Instance`, which is **.NET 5+ only**. `Controls.Core` also targets **netstandard2.0/2.1**, so the shared build breaks: ``` src/Controls/src/Core/GradientBrush.cs(13,71): error CS0122: 'ReferenceEqualityComparer' is inaccessible due to its protection level [Controls.Core.csproj::TargetFramework=netstandard2.0] ``` This blocks every build of `inflight/current` (including Copilot PR reviews on inflight PRs, e.g. #36776, where the gate could not build the base). ### Fix Replace the BCL comparer with a small self-contained reference-identity comparer that works on **every** TFM — equality by object reference, hash from `RuntimeHelpers.GetHashCode` — preserving the leak fix's exact semantics. The other `ReferenceEqualityComparer` uses (`ShellRenderer.cs`, `MapHandler.Android.cs`) are in platform-specific files that only compile for .NET TFMs, so they are unaffected. ### Risk Minimal — behaviorally identical to `ReferenceEqualityComparer.Instance`; no public API change. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 15d2af20-e4ab-4e88-9011-cfbd83513bc0
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
GradientBrush subscribes to each GradientStop's PropertyChanged event, but GradientStops.Clear() raises a collection Reset with e.OldItems == null. Because GradientBrush.OnGradientStopCollectionChanged only unsubscribes stops from e.OldItems, cleared stops remain subscribed.
Description of Change
Added a dictionary to track GradientStop subscriptions and manage event subscriptions correctly. Updated the collection change handling to support all collection actions, including Add, Remove, Replace, Move, and Reset. Fixed the Reset scenario by properly unsubscribing from existing GradientStop instances before re-subscribing to the current collection items. Also added reference counting to correctly handle duplicate GradientStop instances and refactored the collection attach/detach logic into helper methods to improve code maintainability.
Issues Fixed
Fixes #36743
Tested the behavior in the following platforms.
Before.mov
After.mov