Repository navigation
Fix Path.Data and Path.RenderTransform visual tree leaks with shared resources #36097
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
kubaflo
merged 2 commits into
dotnet:inflight/current
from
devanathan-vaithiyanathan:fix-34860
Jun 24, 2026
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
115 changes: 115 additions & 0 deletions
115
src/Controls/tests/Core.UnitTests/Shapes/PathSharedResourcesMemoryLeakTests.cs
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| using System; | ||
| using Microsoft.Maui.Controls.Shapes; | ||
| using Microsoft.Maui.Graphics; | ||
| using Xunit; | ||
|
|
||
| namespace Microsoft.Maui.Controls.Core.UnitTests.Shapes; | ||
|
|
||
| public class PathSharedResourcesMemoryLeakTests : BaseTestFixture | ||
| { | ||
| // NOTE: Path creation MUST be in a separate method. | ||
| // If created inline in the test method, the JIT keeps local variables alive for the | ||
| // entire method scope in debug mode — making the path uncollectable regardless of the fix. | ||
| // This is the standard .NET pattern for writing reliable GC/leak unit tests. | ||
|
|
||
| static WeakReference CreatePathWithGeometry(PathGeometry geometry) | ||
| { | ||
| var path = new Path { Data = geometry }; | ||
| return new WeakReference(path); | ||
| } | ||
|
|
||
| static WeakReference CreatePathWithTransform(ScaleTransform transform) | ||
| { | ||
| var path = new Path { RenderTransform = transform }; | ||
| return new WeakReference(path); | ||
| } | ||
|
|
||
| static WeakReference CreatePathWithBoth(PathGeometry geometry, ScaleTransform transform) | ||
| { | ||
| var path = new Path { Data = geometry, RenderTransform = transform }; | ||
| return new WeakReference(path); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void SharedPathGeometry_DoesNotKeepPathAlive() | ||
| { | ||
| var sharedGeometry = new PathGeometry | ||
| { | ||
| Figures = new PathFigureCollection | ||
| { | ||
| new PathFigure | ||
| { | ||
| StartPoint = new Point(0, 0), | ||
| Segments = new PathSegmentCollection | ||
| { | ||
| new LineSegment { Point = new Point(100, 100) } | ||
| } | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| var pathRef = CreatePathWithGeometry(sharedGeometry); | ||
|
|
||
| GC.Collect(); | ||
| GC.WaitForPendingFinalizers(); | ||
| GC.Collect(); | ||
|
|
||
| Assert.False(pathRef.IsAlive, | ||
| "Path was not collected. The shared PathGeometry is holding a strong reference to " + | ||
| "Path via its PropertyChanged event handler. Use WeakGeometryChangedProxy to fix."); | ||
|
|
||
| // Ensures sharedGeometry is a live GC root during the Collect calls above. | ||
| GC.KeepAlive(sharedGeometry); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void SharedRenderTransform_DoesNotKeepPathAlive() | ||
| { | ||
| var sharedTransform = new ScaleTransform(1.5, 1.5) { CenterX = 50, CenterY = 50 }; | ||
|
|
||
| var pathRef = CreatePathWithTransform(sharedTransform); | ||
|
|
||
| GC.Collect(); | ||
| GC.WaitForPendingFinalizers(); | ||
| GC.Collect(); | ||
|
|
||
| Assert.False(pathRef.IsAlive, | ||
| "Path was not collected. The shared ScaleTransform is holding a strong reference to " + | ||
| "Path via its PropertyChanged event handler. Use WeakNotifyPropertyChangedProxy to fix."); | ||
|
|
||
| GC.KeepAlive(sharedTransform); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void SharedGeometryAndTransform_DoNotKeepPathAlive() | ||
| { | ||
| var sharedGeometry = new PathGeometry | ||
| { | ||
| Figures = new PathFigureCollection | ||
| { | ||
| new PathFigure | ||
| { | ||
| StartPoint = new Point(0, 0), | ||
| Segments = new PathSegmentCollection | ||
| { | ||
| new LineSegment { Point = new Point(50, 50) } | ||
| } | ||
| } | ||
| } | ||
| }; | ||
| var sharedTransform = new ScaleTransform(2.0, 2.0) { CenterX = 25, CenterY = 25 }; | ||
|
|
||
| var pathRef = CreatePathWithBoth(sharedGeometry, sharedTransform); | ||
|
|
||
| GC.Collect(); | ||
| GC.WaitForPendingFinalizers(); | ||
| GC.Collect(); | ||
|
|
||
| Assert.False(pathRef.IsAlive, | ||
| "Path was not collected. Shared PathGeometry and/or ScaleTransform are holding " + | ||
| "strong references to Path via event subscriptions. Use weak proxies to fix."); | ||
|
|
||
| GC.KeepAlive(sharedGeometry); | ||
| GC.KeepAlive(sharedTransform); | ||
| } | ||
| } |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[major] Public API Surface / Build — Adding a destructor to public
Pathintroduces a new finalizer API entry, but the PR does not update the ControlsPublicAPI.Unshipped.txtbaselines. The shipped API files already track destructors forShape,Border, and other public types, so API validation is likely to fail forMicrosoft.Maui.Controls.Shapes.Path.~Path() -> voidon each target. Either add the new destructor entries to the appropriatesrc/Controls/src/Core/PublicAPI/*/PublicAPI.Unshipped.txtfiles, or avoid introducing aPathfinalizer by moving the cleanup to an already-finalized base/type-level cleanup pattern.