[ios] fix memory leak in ImageButton#18602
Merged
jonathanpeppers merged 2 commits intodotnet:mainfrom Nov 10, 2023
Merged
Conversation
Member
Author
|
Looks like the new test fails on Android: |
Context: dotnet#18365 Adding a parameter to the test: [Theory("Handler Does Not Leak")] [InlineData(typeof(ImageButton))] public async Task HandlerDoesNotLeak(Type type) Shows a memory leak in `ImageButton`, caused by the cycle * `ImageButtonHandler` -> * `UIButton` events like `TouchUpInside` -> * `ImageButtonHandler` I could solve this problem by creating a `ImageButtonProxy` class -- the same pattern I've used in other PRs to avoid cycles. This makes an intermediate type to handle the events and breaks the cycle. Still thinking if the analyzer could have caught this, issue filed at: jonathanpeppers/memory-analyzers#12
Context: a270ebd Context: 1bbe79d Context: material-components/material-components-android#2063 Reviewing a GC dump of the device tests, I noticed a `System.Action` keeping the `ImageButton` alive: Microsoft.Maui.Controls.ImageButton System.Action Java.Lang.Thread.RunnableImplementor So next, I looked for `System.Action` and found the path on the `ReferencedTypes` tab: System.Action Microsoft.Maui.Platform.ImageButtonExtensions.[]c__DisplayClass4_0 Google.Android.Material.ImageView.ShapeableImageView Microsoft.Maui.Controls.ImageButton Which led me to the code: public static async void UpdatePadding(this ShapeableImageView platformButton, IImageButton imageButton) { platformButton.SetContentPadding(imageButton); platformButton.Post(() => { platformButton.SetContentPadding(imageButton); }); platformButton.SetContentPadding(imageButton); } ?!? Why is this code calling `SetContentPadding` three times? Reviewing the commit history: * a270ebd * 1bbe79d * material-components/material-components-android#2063 I could comment out the code and the leak is solved, but I found I could also change the code to use `await Task.Yield()` for the same result.
6445ac7 to
d1d2ca0
Compare
jonathanpeppers
commented
Nov 9, 2023
Comment on lines
44
to
52
|
|
||
| public static void UpdatePadding(this ShapeableImageView platformButton, IImageButton imageButton) | ||
| public static async void UpdatePadding(this ShapeableImageView platformButton, IImageButton imageButton) | ||
| { | ||
| platformButton.SetContentPadding(imageButton); | ||
| platformButton.Post(() => | ||
| { | ||
| platformButton.SetContentPadding(imageButton); | ||
| }); | ||
|
|
||
| // see: https://github.com/material-components/material-components-android/issues/2063 | ||
| await Task.Yield(); | ||
| platformButton.SetContentPadding(imageButton); | ||
| } |
Member
Author
There was a problem hiding this comment.
@jsuarezruiz this code was causing a leak, see details on the commit message: d1d2ca0
Will a test fail, if the problem in 1bbe79d comes back?
Contributor
There was a problem hiding this comment.
Yes, there is a specific test to validate the padding: PaddingInitializesCorrectly
jsuarezruiz
approved these changes
Nov 10, 2023
21 tasks
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.
iOS / Catalyst
Context: #18365
Adding a parameter to the test:
Shows a memory leak in
ImageButton, caused by the cycleImageButtonHandler->UIButtonevents likeTouchUpInside->ImageButtonHandlerI could solve this problem by creating a
ImageButtonProxyclass -- the same pattern I've used in other PRs to avoid cycles. This makes an intermediate type to handle the events and breaks the cycle.Still thinking if the analyzer could have caught this, issue filed at:
jonathanpeppers/memory-analyzers#12
Android
Context: a270ebd
Context: 1bbe79d
Context: material-components/material-components-android#2063
Reviewing a GC dump of the device tests, I noticed a
System.Actionkeeping the
ImageButtonalive:So next, I looked for
System.Actionand found the path on theReferencedTypestab:Which led me to the code:
?!? Why is this code calling
SetContentPaddingthree times?Reviewing the commit history:
I could comment out the code and the leak is solved, but I found I could
also change the code to use
await Task.Yield()for the same result.