Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

fix memory leak (and some crashes) in ItemsViewController (iOS) #14111

Merged
merged 3 commits into from
Jul 12, 2021

Conversation

tmijieux
Copy link
Contributor

@tmijieux tmijieux commented Apr 5, 2021

Description of Change

When a CollectionView is removed from a layout or removed from view hierarchy (My use case is the following: when the user rotate the screen the layout changes. So, in my code, I recreate a new layout with a new CollectionView), the BindingContext is set to null, hence the ItemsSourceProperty is set to null and the ItemsSource property in the Xamarin.Forms.Platform.iOS.ItemsViewController will be updated (through ItemsViewRenderer.OnElementPropertyChanged).

The problem is, if the IItemsViewSource is an Xamarin.Forms.Platform.iOS.ObservableItemsSource , the dispose method on the old ItemsSource value is not called and that dispose method is supposed to unsubscribe from the event delegate.
In the end, that object is still subscribed on the ObservableCollection in my ViewModel that has not reached the end of its lifetime(I continue using the same viewmodel), hence the ObservableItemSource is leaked because still referenced by the CollectionChanged event delegate, but the ItemsViewController which is supposed to own the reference to that ObservableItemsSource has no reference anymore.

The change in this PR is:

  • Disposing pre-existing ObservableItemsSource(if any) when setting ItemsSource property to a new value (in ItemsViewController)

Issues Resolved

I did not yet do a complete check of what issues could be affected by this change.
One of the crash i encountered myself was exactly the same as in #13910 (same stacktrace) (when on INotifyCollectionChanged collection call the CollectionChanged event with the Reset event).

The reason this crash was happening is (i think) because the ObservableItemsSource, or the referenced views (if any) are removed from view hierarchy and they are not in a valid state anymore.

I will work on creating an issue with a minimal reproduction for my problem as soon as this PR is submitted.
EDIT: -> #14113

API Changes

None

Platforms Affected

  • iOS

Behavioral/Visual Changes

None

Before/After Screenshots

Not applicable

Testing Procedure

PR Checklist

  • Targets the correct branch
  • Tests are passing (or failures are unrelated)

@jtorvald
Copy link
Contributor

jtorvald commented Apr 6, 2021

Same issue with multiple other leaky items was also fixed in #14076

@jsuarezruiz jsuarezruiz added the i/high Completely doesn't work, crashes, or is unusably slow, has no obvious workaround; occurs less often label Apr 28, 2021
@rachelkang rachelkang added this to In Review in CollectionView via automation Apr 30, 2021
@rachelkang
Copy link
Contributor

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@rmarinho
Copy link
Member

rmarinho commented May 4, 2021

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@rachelkang rachelkang added this to Issue In progress in 5.0.0 SR 4 (Planning) via automation Jun 11, 2021
@Redth Redth moved this from Issue In progress to Meets Bar in 5.0.0 SR 4 (Planning) Jun 28, 2021
@rmarinho
Copy link
Member

rmarinho commented Jul 2, 2021

Is it possible to add a test case so we can make sure it fixes the issue ?

@tmijieux
Copy link
Contributor Author

tmijieux commented Jul 5, 2021

@rmarinho do you mean a unit test ? I'm currently trying to add one. I figured it would land in the Xamarin.Forms.Platform.iOS.UnitTest namespace,... I'm not yet sure how to run the tests that lies in this namespaces(currently updating stuff on my mac server to see if it is possible to run on device, but i'm not sure this is the way to go...)

If just a simple repro project is needed a already linked one with the associated issue:

#14113
https://github.com/tmijieux/XFBugItemsViewController/

@tmijieux tmijieux force-pushed the 5.0.0 branch 2 times, most recently from ae108fe to 06c3d5e Compare July 6, 2021 07:35
@net-foundation-cla
Copy link

net-foundation-cla bot commented Jul 6, 2021

CLA assistant check
All CLA requirements met.

@rmarinho
Copy link
Member

rmarinho commented Jul 6, 2021

I was saying a a sample page on the Issues gallery, and you can add a UITest

@rmarinho rmarinho moved this from Meets Bar to PR Needs Review in 5.0.0 SR 4 (Planning) Jul 6, 2021
@tmijieux
Copy link
Contributor Author

tmijieux commented Jul 6, 2021

i added two test,

  • the first one DoesNotLeakdoes not seem to work yet, (it pass both before and after the fix)
  • the second one DoesNotCrash seem to work as expected (fail before and pass after) .

I was about to look into my first test, but about at the same time i figured i would rebase on the lastest commit of xamarin/Xamarin.Forms:5.0.0 to see if things changed in between, but now i cannot deploy the ControlGallery to my iphone anymore (there is a build error that i can get rid of for now that have to do with a path length issue seemingly 😡 ) i'm not even sure that my deploy issue is related to the rebase or not, but i cant recall on what commit i was based before...

@tmijieux tmijieux changed the title fix memory leak (and some crashes) in ItemsViewController (iOS) [WIP] fix memory leak (and some crashes) in ItemsViewController (iOS) Jul 6, 2021
@tmijieux tmijieux changed the title [WIP] fix memory leak (and some crashes) in ItemsViewController (iOS) fix memory leak (and some crashes) in ItemsViewController (iOS) Jul 6, 2021
@rmarinho
Copy link
Member

rmarinho commented Jul 6, 2021

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@tmijieux
Copy link
Contributor Author

tmijieux commented Jul 6, 2021

Updated the tests, both tests now fail without the fix and pass with the fix.

I was saying a a sample page on the Issues gallery, and you can add a UITest

I'm not sure what you expect exactly, can you give a little more guidance please(is there a guideline I should read) ?

I put my tests in the unit tests because i just mimicked what was done in other unit tests to reproduce my bug, and the code in UITests seemed harder to grasp for me at first. But my feeling about the tests and the bugs is that they are not exactly trivial, so i understand if you want me to take them out of the "unit" tests.

@jsuarezruiz
Copy link
Contributor

The added test are passing.

Captura de pantalla 2021-07-07 a las 11 36 55

@rmarinho rmarinho self-requested a review July 7, 2021 11:14
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.

I think a best fix is calling

ItemsSource?.Dispose();

On the UpdateItemsSource() method

@rmarinho rmarinho self-requested a review July 9, 2021 14:54
@rmarinho rmarinho merged commit 056e30f into xamarin:5.0.0 Jul 12, 2021
CollectionView automation moved this from In Review to Done Jul 12, 2021
5.0.0 SR 4 (Planning) automation moved this from PR Needs Review to Done Jul 12, 2021
rmarinho added a commit that referenced this pull request Sep 20, 2021
* Fix 4143: improved Span region calculation (#13348) fixes #4143 fixes #6992 fixes #11650 fixes #7655 fixes #11657 fixes #10520 fixes #4829 

* improved span region calculation on android

* Remove unused code

* Nit: clean up comments

* Wrap in ScrollView for small screen compatibility

Co-authored-by: Tim Dittmar <Tim.Dittmar@Exxeta.com>
Co-authored-by: Rachel Kang <rachel.j.kang@gmail.com>
Co-authored-by: Rachel Kang <rachelkang@microsoft.com>

* Only check for netfx when not in a wapproj (#14402) fixes #13957

* [macOS] Update Switch renderer (#14334) fixes #14313

* [macOS] Update Switch renderer #14313

* [macOS] Update Switch renderer #14313

* [Android] Fix to Issue Java.Lang.IndexOutOfBoundsException: setSpan (#12764) Fix to Issue #12762

* Update EntryRenderer.cs

Fix to Issue #12762
Java.Lang.IndexOutOfBoundsException: setSpan (nn ... nn) ends beyond length n-1

* Fix to EntryRenderer.cs

* Update EntryRenderer.cs

* Update EntryRenderer.cs

Co-authored-by: Rui Marinho <me@ruimarinho.net>

* [Android] Fix ClearButton not working when changing the ClearButtonVisibility while the Entry field is focused (#13820) Fixes #13819

Co-authored-by: Javier Suárez <javiersuarezruiz@hotmail.com>

* Automated dotnet-format update (#14404)

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* Automated dotnet-format update (#14405)

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* fix memory leak (and some crashes) in ItemsViewController (iOS) (#14111)

* add unit test

* fix memory leak (and some crashes) in ItemsViewController (iOS)

* Update ItemsViewController.cs

Co-authored-by: Rui Marinho <me@ruimarinho.net>

* Enable ScrollTo tests for Android, UWP; implement ScrollTo with group index for Android (#13007)

* Remove inaccurate category

* Enable ScrollTo tests for Android/UWP; Implement grouped ScrollTo for Android

* Implement missing ScrollTo grouped item by index

* Exempt 3788 test from UWP

* Ignore test on UWP

Co-authored-by: Rui Marinho <me@ruimarinho.net>

* Fix type for case blueviolet (#14434)

The 'v' in this case was uppercase causing this typeconverter to reject BlueViolet as a valid color value. Fixes #AB1286706 [XVS]XLS0431: Invalid value for property 'BackgroundColor': 'BlueViolet'

* [iOS] Fix: CollectionView was not updating when it was invisible (#14384)

* [iOS] Fix: CollectionView was not updating when it was invisible

* [iOS] Fix: NSInternalInconsistencyException Reason: request for number of items in section X when there are only X sections in the collection view when NumberOfItemsInSection is invoked

* Revert "[iOS] Fix: NSInternalInconsistencyException Reason: request for number of items in section X when there are only X sections in the collection view when NumberOfItemsInSection is invoked"

This reverts commit 2343f15.

* [iOS] Fix: Prevent NumberOfItemsInSection invoking if CollectionView is hidded to avoid  "NSInternalInconsistencyException Reason: request for number of items in section X when there are only X sections"

* [iOS] Scroll locked issue using SwipeView (#12758)

* Fixed issue re-enabling the scroll

* Added repro sample

* Fixed also the swipe sensitive issue on iOS

Co-authored-by: Gerald Versluis <gerald.versluis@microsoft.com>

* Fix removed shadow in Thumb setting a custom color (#13166)

* [iOS] Fix crash/closing wrong modal with FormSheet and tap outside (#14527)

* Add repro + fix

* Update Issue12300.cs

* [Core] SwipeItem Parent using SwipeView in DataTemplate (#13385)

* Added repro sample

* Fix the issue

Co-authored-by: Gerald Versluis <gerald.versluis@microsoft.com>

* Fix Frame Background issue with transparent color (#14565)

* Fix broken disabled Button visual state in UWP (#14567)

Co-authored-by: Gerald Versluis <gerald.versluis@microsoft.com>

* Update Device.Idiom when flipping tablet mode state (#13150)

* [Android] Fix occasional wrong touch interception in SwipeView Content  (#13732)

* Fix wrong touch interception in SwipeView Content on Android

* Updated swipe delta

* Fix Entry issue using TextColor and ClearButton in iOS < 13 (#14566)

Co-authored-by: Gerald Versluis <gerald.versluis@microsoft.com>

* Fix issue using FormattedString and LineBreakMode on iOS (#14572)

Co-authored-by: Gerald Versluis <gerald.versluis@microsoft.com>

* Align BarBackground behavior between Android and iOS. (#13503)

* Automated dotnet-format update (#14570)

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* [Android] Update gradients based on offset changes in Frame and BoxView (#11812)

* Update gradients based on offset changes in Frame and BoxView on Android

* Updated the sample to test also the BoxView Brush offsets

* Fix NRE in ListView Command validation (#14580)

* [iOS] Fix inability to check an initially disabled RadioButton after enabling it (#14545)

* Listen for updates to IsEnabled on RadioButton to ensure that the tap gesture recogniser is added when the control is enabled

* Update Issue14544.cs

* Update test case

Co-authored-by: Gerald Versluis <gerald@verslu.is>

* Fix crash navigating in Shell (#14577)

* Androidx bumps (#14506)

* Update to Latest Android X Libraries

* - fix android forwarders

* Moar updates

Co-authored-by: Gerald Versluis <gerald@verslu.is>

* Validate issue 14433 (#14576)

* Added repro sample

* Updated sample

Co-authored-by: Gerald Versluis <gerald.versluis@microsoft.com>

* [iOS] Remove usage of System.Drawing types (#14571)

* Run nightly also for 5.0.0 branch

* Automated dotnet-format update (#14581)

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* Ordering children while adding (#8231)

* Ordering children while adding

* Abstract OrderElement

* Re-order on runtime child addition and Tests

* Runtime addition ensures Z Index

* Stop performance measurement on return

* Update VisualElementPackager.cs

Co-authored-by: Gerald Versluis <gerald@verslu.is>
Co-authored-by: Javier Suárez <javiersuarezruiz@hotmail.com>

* If stroke is null avoid render a shape stroke (#14587)

* Fix broken Android platform tests (#14590)

* Automated dotnet-format update (#14595)

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* [macOS] Fix RadioButton activated behaviour (#14139)

* Respond to Activated event

* Set initial control state based on element state

* Revert "Set initial control state based on element state"

This reverts commit 1bd1238.

* CollectionView RemainingItemsThreshold implementation for UWP (#14202)

* [Android] Fix building with Android stable bits (#14608)

* Set java sdk path

* Use class-parse

Co-authored-by: Braini01 <T_Dittmar@web.de>
Co-authored-by: Tim Dittmar <Tim.Dittmar@Exxeta.com>
Co-authored-by: Rachel Kang <rachel.j.kang@gmail.com>
Co-authored-by: Rachel Kang <rachelkang@microsoft.com>
Co-authored-by: Matthew Leibowitz <mattleibow@live.com>
Co-authored-by: MH Rastegari <42671084+mhrastegary77@users.noreply.github.com>
Co-authored-by: Felipe Silveira <felipe@transis.com.br>
Co-authored-by: Rui Marinho <me@ruimarinho.net>
Co-authored-by: Jonathan Goyvaerts <jonathan.goyvaerts@gmail.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Thomas Mijieux <tmijieux@users.noreply.github.com>
Co-authored-by: E.Z. Hart <hartez@users.noreply.github.com>
Co-authored-by: Marco Goertz <mgoertz@microsoft.com>
Co-authored-by: Kirill <rotorgames@bk.ru>
Co-authored-by: Gerald Versluis <gerald.versluis@microsoft.com>
Co-authored-by: Lee Millward <lee.millward@gmail.com>
Co-authored-by: Gerald Versluis <gerald@verslu.is>
Co-authored-by: Shane Neuville <shneuvil@microsoft.com>
Co-authored-by: Filip Navara <filip.navara@gmail.com>
Co-authored-by: Shanmukha Ranganath <shanmukharanganath@gmail.com>
Co-authored-by: Julio César Rocha <JunielKatarn@users.noreply.github.com>
Co-authored-by: nk221 <kirill.n@gmail.com>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
a/collectionview i/high Completely doesn't work, crashes, or is unusably slow, has no obvious workaround; occurs less often p/iOS 🍎
Projects
Development

Successfully merging this pull request may close these issues.

None yet

6 participants