Skip to content

[Windows] Fix CollectionView CV2 retention after dynamic removal - #39543

Merged
kubaflo merged 1 commit into
dotnet:net11.0from
sheiksyedm:sheiksyedm-cv2-memory-retention-fix
Oct 22, 2026
Merged

kubaflo merged 1 commit into
dotnet:net11.0from
sheiksyedm:sheiksyedm-cv2-memory-retention-fix

Conversation

@sheiksyedm

Copy link
Copy Markdown
Contributor

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

Dynamically removing a CollectionView from a page could leave the managed CollectionView, its handler, native collection control, and realized item content alive after repeated garbage collection and UI idle.

In the source-built baseline, 180 dynamic add/remove cycles retained all 180 removed native collection controls. The host page remained open during this scenario.

The investigation identified several retaining paths:

  • WinUI's COM projection retained the item factory, which strongly referenced the MAUI ItemsView.
  • Templated source adapters strongly referenced their owning ItemsView.
  • Collection-change subscriptions could retain the handler or grouped adapter.
  • A retained item factory could keep recycled containers and their managed rows alive through its recycle pool, even after the owner became collectible.

Fresh navigation-only testing did not show the same progressive native retention. Dynamic removal while the host remained alive was the primary reproduced failure.

Fix details

  • Reference the ItemsView weakly from the item factory and templated source adapters.
  • Pass the adapter's weak-owner reference into generated item contexts.
  • Use weak collection-change proxies for the handler and grouped-source subscriptions.
  • Keep callback delegates alive for as long as their subscriber remains alive, preserving live updates across GC.
  • Store recycle pools in a ConditionalWeakTable keyed by the owning ItemsView. Recycled containers remain reusable while the owner is alive, but a retained factory no longer independently extends their lifetime.

The fix does not require application-side handler disconnection or manual cleanup. Existing virtualization, layout, template selection, and recycling algorithms are preserved.

Break strong owner and collection-change retention paths in Windows CV2. Tie recycled item pools to the CollectionView lifetime while preserving reuse for live owners.

Add Windows CV2 device regressions for removal, grouped subscriptions, reattachment, and recycled item lifetime and rebinding.
Copilot AI balanced review requested due to automatic review settings October 22, 2026 08:19
@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 -- 39543

Or

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

@sheiksyedm
sheiksyedm changed the base branch from main to net11.0 October 22, 2026 08:19
@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

Skill Validation Results

@sheiksyedm — new skill validation results are available based on this last commit: 46ccc66.
To request a fresh validation after new comments or commits, comment /evaluate-skills.

Overall Failed Static Failed LLM Needs attention Skills 0 Agents 0

❌ Skill Validation Results — 46ccc66 · [Windows] Fix CollectionView CV2 retention after dynamic removal · 2026-10-22T08:20:06Z

❌ Static Checks Failed

Skills: 0 | Eval specs linted:

⏭️ LLM Evaluation: Skipped

No changed skills with eval specs found.

🔍 Full results and investigation steps

@dotnet-policy-service dotnet-policy-service Bot added the partner/syncfusion Issues / PR's with Syncfusion collaboration label Oct 22, 2026
@sheiksyedm sheiksyedm added community ✨ Community Contribution platform/windows area-controls-collectionview CollectionView, CarouselView, IndicatorView collectionview-winui-cv2 labels Oct 22, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 Needs a closer look

Windows COM and garbage-collection lifetime changes need device-test validation and human review, with handler proxy cleanup still unresolved.

1 open issue
What changed in this PR

This PR addresses Windows CollectionView handler retention after dynamic removal while the host page remains open.

Changes:

  • Replace strong owner references and collection-change subscriptions with weak references and proxies.
  • Tie recycle-pool lifetime to the owning CollectionView.
  • Add device tests for collection, reattachment, live updates, and recycling.
File Description
src/​Controls/​tests/​DeviceTests/​Elements/​CollectionView/​CollectionView2MemoryTests.Windows.cs Adds lifetime and continued-functionality regression tests.
src/​Controls/​src/​Core/​Handlers/​Items2/​Windows/​ObservableItemTemplateCollection2.cs Weakly references the owner and guards notifications.
src/​Controls/​src/​Core/​Handlers/​Items2/​Windows/​ItemTemplateContextList2.cs Uses a weak owner reference for cached contexts.
src/​Controls/​src/​Core/​Handlers/​Items2/​Windows/​ItemTemplateContextEnumerable2.cs Passes a weak owner reference into enumerated contexts.
src/​Controls/​src/​Core/​Handlers/​Items2/​Windows/​ItemTemplateContext2.cs Supports sharing an existing weak owner reference.
src/​Controls/​src/​Core/​Handlers/​Items2/​Windows/​ItemFactory.cs Weakly references the owner and scopes recycle-pool retention to its lifetime.
src/​Controls/​src/​Core/​Handlers/​Items2/​Windows/​GroupedItemTemplateCollection2.cs Adds weak subscriptions, retained callbacks, and finalizer cleanup.
src/​Controls/​src/​Core/​Handlers/​Items2/​ItemsViewHandler2.Windows.cs Replaces direct collection-change subscriptions with a weak proxy.

Review effort: Balanced

Comment on lines +70 to +71
WeakNotifyCollectionChangedProxy? _itemsChangedProxy;
NotifyCollectionChangedEventHandler? _itemsChanged;
@github-actions github-actions Bot added the s/agent-review-in-progress AI review is currently running for this PR label Oct 22, 2026
@kubaflo

This comment has been minimized.

@azure-pipelines

This comment has been minimized.

@kubaflo
kubaflo merged commit b039b2b into dotnet:net11.0 Oct 22, 2026
135 of 194 checks passed
@github-actions github-actions Bot added this to the .NET 11.0-rc2 milestone Oct 22, 2026

This branch was previously deployed

1 inactive deployment
copilot-pat-pool — 46ccc665 Deployed Oct 22, 2026 by sheiksyedm via conclusion #2446
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-controls-collectionview CollectionView, CarouselView, IndicatorView collectionview-winui-cv2 community ✨ Community Contribution partner/syncfusion Issues / PR's with Syncfusion collaboration platform/windows s/agent-review-in-progress AI review is currently running for this PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants