Skip to content

Conversation

@bhavanesh2001
Copy link
Contributor

@bhavanesh2001 bhavanesh2001 commented May 23, 2025

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!

Description of Change

This PR resolves #29619 by eliminating the memory leak caused by CollectionViewHandler2's subscription to ItemsLayout property changes and reworking the property change flow.

Problems

  • Previously, we used a static LinearItemsLayout.Vertical instance as the default for ItemsLayout
  • When the handler subscribed to property changes on this shared instance (without unsubscribing), it caused memory leaks (in the CV2 case)
  • Additionally, Android and Windows used WeakNotifyPropertyChangedProxy to work around this, but that added platform complexity and only masked the root cause.

Fix

  • The default ItemsLayout is now created per-instance using defaultValueCreator, not a static object.
  • The ItemsView (virtual view) subscribes directly to changes on its ItemsLayout and raises OnPropertyChanged("ItemsLayout") for relevant internal changes (Span, ItemSpacing, etc.).
  • This triggers the MapItemsLayout mapper, which applies platform-specific updates.
  • Removed WeakNotifyPropertyChangedProxy from Android and Windows for ItemsLayout changes.
  • All layout updates now flow through the mapper via the virtual view

Supersedes to #29635 , #28675 , #29190, #28311

Issues Fixed

Fixes #29619
Fixes #27666
Fixes #27667
Fixes #28656
Fixes #29696
Fixes #28023
Fixes #23377

@bhavanesh2001 bhavanesh2001 requested a review from a team as a code owner May 23, 2025 11:41
@dotnet-policy-service dotnet-policy-service bot added the community ✨ Community Contribution label May 23, 2025
@bhavanesh2001 bhavanesh2001 marked this pull request as draft May 23, 2025 11:44
Copy link
Member

@rmarinho rmarinho left a comment

Choose a reason for hiding this comment

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

Can we add the CV2 to the leak tests ?

@jsuarezruiz jsuarezruiz added the area-controls-collectionview CollectionView, CarouselView, IndicatorView label May 26, 2025
@bhavanesh2001
Copy link
Contributor Author

bhavanesh2001 commented May 26, 2025

While enabling memory tests for CV2, I found that CarouselViewHandler2 is leaking 😢. It seems the cause is different from the issue we're addressing here — something else is triggering the leak.

Anyway, I’ll definitely look into it.

Aside from that, everything here looks great.

@rmarinho
Copy link
Member

/azp run

@dotnet dotnet deleted a comment from azure-pipelines bot May 28, 2025
@dotnet dotnet deleted a comment from jsuarezruiz May 28, 2025
@azure-pipelines
Copy link

Azure Pipelines successfully started running 3 pipeline(s).

@dotnet dotnet deleted a comment from azure-pipelines bot May 28, 2025
@dotnet dotnet deleted a comment from PureWeen May 28, 2025
@dotnet dotnet deleted a comment from azure-pipelines bot May 28, 2025
@dotnet dotnet deleted a comment from PureWeen May 28, 2025
@bhavanesh2001
Copy link
Contributor Author

bhavanesh2001 commented May 29, 2025

Only CarouselViewHandler2 memory tests are failing. #29719 should fix the leak.

I also noticed that CarouselView too uses a static instance as the default ItemsLayout. I'm updating it to use a per-instance value via defaultValueCreator and managing property changes in the VirtualView itself,just like we did with ItemsView for CollectionView.

Also, I need to write few UI tests to verify that property changes in ItemsLayout (e.g., ItemSpacing) are correctly reflected in the UI for both CollectionView and CarouselView

@rmarinho
Copy link
Member

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 3 pipeline(s).


/// <summary>Bindable property for <see cref="ItemsLayout"/>.</summary>
public static readonly BindableProperty ItemsLayoutProperty =
BindableProperty.Create(nameof(ItemsLayout), typeof(LinearItemsLayout), typeof(ItemsView),
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@rmarinho, shouldn't the declaring type here be typeof(CarouselView)?

Copy link
Member

Choose a reason for hiding this comment

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

Yes it should be

@bhavanesh2001 bhavanesh2001 force-pushed the fix_collectionview_ItemsLayout branch from f11c626 to 37aba59 Compare May 29, 2025 17:05
@jsuarezruiz
Copy link
Contributor

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 3 pipeline(s).

@PureWeen
Copy link
Member

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 3 pipeline(s).

@bhavanesh2001 bhavanesh2001 force-pushed the fix_collectionview_ItemsLayout branch from 83996c6 to b047f48 Compare July 15, 2025 18:03
@PureWeen PureWeen modified the milestones: .NET 9 SR10, .NET 9 SR11 Aug 4, 2025
@bhavanesh2001 bhavanesh2001 force-pushed the fix_collectionview_ItemsLayout branch from b047f48 to 89a8850 Compare August 18, 2025 13:56
@baaaaif
Copy link

baaaaif commented Aug 26, 2025

Is it realistic to assume that this will come in SR11, related to the milestone?

@PureWeen PureWeen modified the milestones: .NET 9 SR11, .NET 9 SR12 Sep 10, 2025
@PureWeen
Copy link
Member

#29683

@PureWeen PureWeen closed this Sep 11, 2025
@github-project-automation github-project-automation bot moved this from Ready To Review to Done in MAUI SDK Ongoing Sep 11, 2025
@github-actions github-actions bot locked and limited conversation to collaborators Oct 12, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-controls-collectionview CollectionView, CarouselView, IndicatorView community ✨ Community Contribution p/0 Current heighest priority issues that we are targeting for a release.

Projects

Status: Done

5 participants