Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions src/Controls/src/Core/Shapes/PathFigure.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ namespace Microsoft.Maui.Controls.Shapes
[ContentProperty("Segments")]
public sealed class PathFigure : BindableObject, IAnimatable
{
readonly WeakNotifyCollectionChangedProxy _segmentsCollectionProxy = new();
readonly List<PathSegment> _subscribedSegments = new();
NotifyCollectionChangedEventHandler _segmentsCollectionChanged;

/// <summary>
/// Initializes a new instance of the <see cref="PathFigure"/> class.
Expand All @@ -24,6 +26,8 @@ public PathFigure()
Segments = new PathSegmentCollection();
}

~PathFigure() => _segmentsCollectionProxy.Unsubscribe();

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

🔍 AI-Generated Review (multi-model)

[major] Memory Leak Prevention — The finalizer only unsubscribes the collection-level weak proxy, but it never detaches PathSegment.PropertyChanged handlers tracked in _subscribedSegments. Concrete failure: if Segments contains one or more rooted segments (shared collection), each segment keeps a strong delegate to PathFigure, so PathFigure stays alive and this finalizer never runs.


/// <summary>Bindable property for <see cref="Segments"/>.</summary>
public static readonly BindableProperty SegmentsProperty =
BindableProperty.Create(nameof(Segments), typeof(PathSegmentCollection), typeof(PathFigure), null,
Expand Down Expand Up @@ -98,14 +102,18 @@ public void BatchCommit()

void UpdatePathSegmentCollection(PathSegmentCollection oldCollection, PathSegmentCollection newCollection)
{
oldCollection?.CollectionChanged -= OnPathSegmentCollectionChanged;
if (oldCollection is not null)
{
_segmentsCollectionProxy.Unsubscribe();
}

UnsubscribeFromAllPathSegmentPropertyChanged();

if (newCollection == null)
return;

newCollection.CollectionChanged += OnPathSegmentCollectionChanged;
_segmentsCollectionChanged ??= OnPathSegmentCollectionChanged;
_segmentsCollectionProxy.Subscribe(newCollection, _segmentsCollectionChanged);

foreach (var newPathSegment in newCollection)
{
Expand All @@ -117,10 +125,7 @@ void OnPathSegmentCollectionChanged(object sender, NotifyCollectionChangedEventA
{
if (e.Action == NotifyCollectionChangedAction.Reset)
{
for (int i = _subscribedSegments.Count - 1; i >= 0; i--)
{
UnsubscribeFromPathSegmentPropertyChanged(_subscribedSegments[i]);
}
UnsubscribeFromAllPathSegmentPropertyChanged();

if (sender is PathSegmentCollection pathSegmentCollection)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,30 @@ static WeakReference CreatePathWithBoth(PathGeometry geometry, ScaleTransform tr
return new WeakReference(path);
}

static WeakReference CreatePathFigureWithSegments(PathSegmentCollection segments)
{
var figure = new PathFigure { Segments = segments };
return new WeakReference(figure);
}

[Fact]
public void SharedPathSegmentCollection_DoesNotKeepPathFigureAlive()
{
var sharedSegments = new PathSegmentCollection();

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

🔍 AI-Generated Review (multi-model)

[major] Regression Prevention and Test Coverage — This regression test uses an empty PathSegmentCollection, so it does not exercise the non-empty path where PathFigure subscribes to each PathSegment.PropertyChanged. Concrete gap: a shared collection with at least one segment can still retain PathFigure through segment event handlers, but this test would pass.


var figureRef = CreatePathFigureWithSegments(sharedSegments);

GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();

Assert.False(figureRef.IsAlive,
"PathFigure was not collected. The shared PathSegmentCollection is holding a strong " +
"reference to PathFigure via its CollectionChanged event handler.");

GC.KeepAlive(sharedSegments);
}

[Fact]
public void SharedPathGeometry_DoesNotKeepPathAlive()
{
Expand Down
Loading