Skip to content

Use weak events for shape and geometry child collections - #38403

Closed
wbv-profile wants to merge 1 commit into
dotnet:mainfrom
wbv-profile:fix/weak-events-shapes
Closed

wbv-profile wants to merge 1 commit into
dotnet:mainfrom
wbv-profile:fix/weak-events-shapes

Conversation

@wbv-profile

Copy link
Copy Markdown
Contributor

Description of Change

TransformGroup, GeometryGroup, PathFigure and PathGeometry each subscribe to a child collection and to every child inside it using plain CLR event handlers, and only unsubscribe when the owning bindable property is replaced. A TransformCollection / GeometryCollection / PathSegmentCollection / PathFigureCollection that outlives its owner — shared between owners, or declared in a ResourceDictionary — therefore roots every owner it is ever assigned to, along with everything those owners reference.

This is the same defect and the same fix as #38397 (GradientBrush), applied to the four remaining shape/geometry types. Each subscription now goes through the existing weak-event primitives in src/Controls/src/Core/Internals/WeakEventProxy.cs, matching the pattern already used by Border and by Shape in this same folder, and each type gains the finalizer those proxies require.

Per type:

  • TransformGroup.Children — collection subscription plus the refcounted per-transform PropertyChanged. The refcount dictionary now carries a WeakNotifyPropertyChangedProxy alongside the count.
  • GeometryGroup.Children — same shape as TransformGroup.
  • PathFigure.Segments — the List<PathSegment> of subscribed segments becomes a Dictionary<PathSegment, WeakNotifyPropertyChangedProxy>; Reset handling is unchanged in behaviour.
  • PathGeometry.Figures — subscribes to each figure twice: PropertyChanged and the internal PathFigure.InvalidatePathSegmentRequested. Making only the first weak would leave the figure still rooting the geometry, so this adds a WeakPathFigureInvalidateProxy to WeakEventProxy.cs, mirroring the existing WeakGeometryChangedProxy exactly. Both subscriptions per figure are now weak.

None of these four types set Parent on their children, so the event subscriptions are the only retention path — unlike GradientBrush, where GradientStop.Parent is a strong reference by design.

Existing Reset / refcount semantics are preserved throughout; the only behavioural change is that a discarded owner no longer stays alive.

Tests

src/Controls/tests/Core.UnitTests/Shapes/ShapesWeakEventTests.cs adds 10 tests: one shared-collection leak test per type, plus six guards asserting that child property changes and collection changes still raise invalidation (TransformGroup recomputing its matrix, GeometryGroup.InvalidateGeometryRequested, PathFigure.InvalidatePathSegmentRequested, and PathGeometry.InvalidatePathGeometryRequested for both a figure change and a segment change nested inside a figure).

The guards matter: without them the four leak tests would also pass if the subscriptions had simply been dropped rather than made weak. Verified by reverting the src/Controls/src/Core changes and re-running — the four leak tests fail and all six guards pass. With the fix all 10 pass and the full Controls.Core.UnitTests suite is green.

Issues Fixed

The leak scanner filed each of these twice; both descriptions refer to the same collection subscription.

Fixes #37582
Fixes #37583
Fixes #37584
Fixes #37585
Fixes #38284
Fixes #38285
Fixes #38286
Fixes #38287

TransformGroup, GeometryGroup, PathFigure and PathGeometry each
subscribe to a child collection and to every child in it with plain CLR
event handlers, unsubscribing only when the owning bindable property is
replaced. A collection that outlives its owner -- shared between owners
or declared in a ResourceDictionary -- therefore roots every owner it is
assigned to.

Each subscription now goes through the weak-event proxies in
Internals/WeakEventProxy.cs, matching the pattern already used by Border
and by Shape in this same folder, and each type gains the finalizer
those proxies require.

PathGeometry subscribes to each figure twice: PropertyChanged and the
internal PathFigure.InvalidatePathSegmentRequested. Making only the
first weak would leave the figure rooting the geometry, so this adds
WeakPathFigureInvalidateProxy, mirroring the existing
WeakGeometryChangedProxy.

Existing Reset and refcount semantics are preserved; the only
behavioural change is that a discarded owner no longer stays alive.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

@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 -- 38403

Or

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

@dotnet-policy-service dotnet-policy-service Bot added the community ✨ Community Contribution label Sep 10, 2026
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Hey there @@wbv-profile! Thank you so much for your PR! Someone from the team will get assigned to your PR shortly and we'll get it reviewed.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Hey there @wbv-profile! Thank you so much for your PR! Someone from the team will get assigned to your PR shortly and we'll get it reviewed.

@kubaflo

kubaflo commented Sep 10, 2026

Copy link
Copy Markdown
Contributor

Worth keeping, but not ready to merge as-is. inflight/current already covers GeometryGroup.Children through #36526; the TransformGroup, PathFigure, and PathGeometry fixes here add meaningful coverage beyond that existing work.

There is a regression in PathGeometry.cs:226-249: the new dictionary stores one subscription per unique PathFigure, while removal unconditionally deletes that subscription. With Figures = { figure, figure }, removing one occurrence disconnects both property-change and segment-invalidation notifications even though the second occurrence remains. The previous implementation subscribed per occurrence. Please retain/ref-count the subscription until the final occurrence is removed, and add a regression that mutates the remaining figure and its segments after one removal.

Please also reconcile the GeometryGroup portion with #36526 rather than replacing the existing inflight fix blindly. No maui-pr, maui-pr-devicetests, or maui-pr-uitests runs were found for this PR; the passing CLA check does not establish build or runtime readiness. The code fix and focused regression should precede merge consideration.

@kubaflo

kubaflo commented Sep 10, 2026

Copy link
Copy Markdown
Contributor

Following up on the review above: closing this version rather than taking the duplicate-PathFigure notification regression. Removing one occurrence must not disconnect notifications while another occurrence remains.

The GeometryGroup portion also overlaps #36526 in inflight/current. The remaining TransformGroup, PathFigure, and PathGeometry leak fixes are still useful and can come back as focused changes with the subscription semantics preserved. This closure does not mean those remaining leaks are already fixed.

@kubaflo kubaflo closed this Sep 10, 2026
@github-actions github-actions Bot locked and limited conversation to collaborators Nov 27, 2026

This branch was previously deployed

1 inactive deployment
copilot-pat-pool — c06f5c61 Deployed Sep 10, 2026 by wbv-profile via conclusion #1840
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.