Toggling Header/Footer in CollectionView Dynamically throws an exception#25743
Merged
rmarinho merged 3 commits intodotnet:mainfrom Nov 18, 2024
Merged
Toggling Header/Footer in CollectionView Dynamically throws an exception#25743rmarinho merged 3 commits intodotnet:mainfrom
rmarinho merged 3 commits intodotnet:mainfrom
Conversation
… in CollectionView Dynamically
Contributor
|
Hey there @Vignesh-SF3580! Thank you so much for your PR! Someone from the team will get assigned to your PR shortly and we'll get it reviewed. |
Contributor
|
/azp run |
|
Azure Pipelines successfully started running 3 pipeline(s). |
jsuarezruiz
suggested changes
Nov 11, 2024
Contributor
|
/azp run |
|
Azure Pipelines successfully started running 3 pipeline(s). |
rmarinho
reviewed
Nov 12, 2024
| uiView?.Dispose(); | ||
| uiView = null; | ||
|
|
||
| formsElement?.Handler?.DisconnectHandler(); |
Member
There was a problem hiding this comment.
Do we need this fix on ItemsViewController2 ?
Contributor
There was a problem hiding this comment.
@rmarinho ,
I've tested this scenario with CollectionViewHandler2, and since the header and footer handling differs in that implementation, the issue does not occur. Dynamically setting the header, footer, and empty view to null works without any exceptions in CollectionViewHandler2, so this fix isn't needed there.
rmarinho
approved these changes
Nov 18, 2024
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
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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.
Root Cause
The issue occurred when toggling the header template in a
CollectionView. Specifically, when the header was set tonulland then re-assigned, anObjectDisposedExceptionwas thrown. This happened because the platform UIView associated with the header was disposed when toggled off, but the handler for theVisualElementwas not cleared, leading to a stale reference.Description of Change
To resolve the issue, I utilized the
DisconnectHandler()method provided by MAUI, which cleanly disconnects the handler from its associated platform view. This ensures that all resources tied to the handler are properly released, preventing any stale references. After callingDisconnectHandler(),formsElementis set tonullto fully clear the reference, allowing the framework to initialize a fresh handler as needed. This approach maintains a consistent view lifecycle and prevents exceptions caused by attempting to access disposed views.Tested the behaviour in the following platforms
Issues Fixed
Fixes #25724
Screenshots
BeforeFix.mov
Afterfix.mov