Skip to content

[ios] fix memory leak in ImageButton#18602

Merged
jonathanpeppers merged 2 commits intodotnet:mainfrom
jonathanpeppers:ImageButtonLeaks
Nov 10, 2023
Merged

[ios] fix memory leak in ImageButton#18602
jonathanpeppers merged 2 commits intodotnet:mainfrom
jonathanpeppers:ImageButtonLeaks

Conversation

@jonathanpeppers
Copy link
Copy Markdown
Member

@jonathanpeppers jonathanpeppers commented Nov 7, 2023

iOS / Catalyst

Context: #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

Android

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:

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.

@jonathanpeppers jonathanpeppers added perf/memory-leak 💦 Memory usage grows / objects live forever (sub: perf) platform/ios labels Nov 7, 2023
@jonathanpeppers jonathanpeppers added this to the .NET 8 + Servicing milestone Nov 7, 2023
@Eilon Eilon added the legacy-area-controls Label, Button, CheckBox, Slider, Stepper, Switch, Picker, Entry, Editor label Nov 8, 2023
@jonathanpeppers jonathanpeppers marked this pull request as ready for review November 8, 2023 02:15
@jonathanpeppers jonathanpeppers requested a review from a team as a code owner November 8, 2023 02:15
@jonathanpeppers
Copy link
Copy Markdown
Member Author

Looks like the new test fails on Android: Microsoft.Maui.Controls.ImageButton should not be alive!

@jonathanpeppers jonathanpeppers marked this pull request as draft November 8, 2023 15:36
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.
@jonathanpeppers jonathanpeppers marked this pull request as ready for review November 9, 2023 15:34
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);
}
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

@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?

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.

Yes, there is a specific test to validate the padding: PaddingInitializesCorrectly

@jonathanpeppers jonathanpeppers merged commit fabfc55 into dotnet:main Nov 10, 2023
@jonathanpeppers jonathanpeppers deleted the ImageButtonLeaks branch November 10, 2023 14:52
@github-actions github-actions bot locked and limited conversation to collaborators Dec 11, 2023
@Eilon Eilon added area-controls-button Button, ImageButton and removed legacy-area-controls Label, Button, CheckBox, Slider, Stepper, Switch, Picker, Entry, Editor labels May 13, 2024
@samhouts samhouts added the fixed-in-8.0.6 Look for this fix in 8.0.6 SR1! label Aug 2, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-controls-button Button, ImageButton fixed-in-8.0.6 Look for this fix in 8.0.6 SR1! perf/memory-leak 💦 Memory usage grows / objects live forever (sub: perf) platform/android platform/ios

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants