Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ios] fix memory leak in Picker #16685

Merged
merged 1 commit into from
Aug 15, 2023
Merged

Conversation

jonathanpeppers
Copy link
Member

Context: #16346

This addresses the memory leak discovered by:

src/Core/src/Platform/iOS/MauiPicker.cs(21,24): error MA0002: Member 'UIPickerView' could cause memory leaks in an NSObject subclass. Remove the member, store the value as a WeakReference, or add the [UnconditionalSuppressMessage("Memory", "MA0002")] attribute with a justification as to why the member will not leak.

I could reproduce a leak in MemoryTests.cs:

++[InlineData(typeof(Picker))]
public async Task HandlerDoesNotLeak(Type type)

Solved the problem by:

  • Introduce MauiPickerProxy for all event subscriptions. Same pattern as in other PRs.

  • The ShouldBeginEditing event was originally subscribed in CreatePlatformView() and never unsubscribed. I moved to be subscribed/unsubscribed the same way as the other events.

  • Refactored the CreateAlert() method to be DisplayAlert() instead. This allows the handler to do all the work -- and the MauiPickerProxy type can call a method on PickerHandler once.

Context: dotnet#16346

This addresses the memory leak discovered by:

    src/Core/src/Platform/iOS/MauiPicker.cs(21,24): error MA0002: Member 'UIPickerView' could cause memory leaks in an NSObject subclass. Remove the member, store the value as a WeakReference, or add the [UnconditionalSuppressMessage("Memory", "MA0002")] attribute with a justification as to why the member will not leak.

I could reproduce a leak in `MemoryTests.cs`:

    ++[InlineData(typeof(Picker))]
    public async Task HandlerDoesNotLeak(Type type)

Solved the problem by:

* Introduce `MauiPickerProxy` for all event subscriptions. Same pattern
  as in other PRs.

* The `ShouldBeginEditing` event was originally subscribed in
  `CreatePlatformView()` and never unsubscribed. I moved to be
  subscribed/unsubscribed the same way as the other events.

* Refactored the `CreateAlert()` method to be `DisplayAlert()` instead.
  This allows the handler to do all the work -- and the
  `MauiPickerProxy` type can call a method on `PickerHandler` once.
@jonathanpeppers jonathanpeppers added platform/iOS 🍎 memory-leak 💦 Memory usage grows / objects live forever (sub: perf) labels Aug 10, 2023
@jonathanpeppers jonathanpeppers marked this pull request as ready for review August 11, 2023 16:27
@jonathanpeppers jonathanpeppers requested a review from a team as a code owner August 11, 2023 16:27
@jfversluis jfversluis merged commit 844b019 into dotnet:main Aug 15, 2023
39 checks passed
@jonathanpeppers jonathanpeppers deleted the PickerOhNo branch August 15, 2023 19:19
rmarinho pushed a commit that referenced this pull request Aug 19, 2023
Context: #16346

This addresses the memory leak discovered by:

    src/Core/src/Platform/iOS/MauiPicker.cs(21,24): error MA0002: Member 'UIPickerView' could cause memory leaks in an NSObject subclass. Remove the member, store the value as a WeakReference, or add the [UnconditionalSuppressMessage("Memory", "MA0002")] attribute with a justification as to why the member will not leak.

I could reproduce a leak in `MemoryTests.cs`:

    ++[InlineData(typeof(Picker))]
    public async Task HandlerDoesNotLeak(Type type)

Solved the problem by:

* Introduce `MauiPickerProxy` for all event subscriptions. Same pattern
  as in other PRs.

* The `ShouldBeginEditing` event was originally subscribed in
  `CreatePlatformView()` and never unsubscribed. I moved to be
  subscribed/unsubscribed the same way as the other events.

* Refactored the `CreateAlert()` method to be `DisplayAlert()` instead.
  This allows the handler to do all the work -- and the
  `MauiPickerProxy` type can call a method on `PickerHandler` once.
@github-actions github-actions bot locked and limited conversation to collaborators Dec 7, 2023
@samhouts samhouts added the fixed-in-8.0.0-rc.1.9171 Look for this fix in 8.0.0-rc.1.9171 label Aug 2, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
fixed-in-8.0.0-rc.1.9171 Look for this fix in 8.0.0-rc.1.9171 memory-leak 💦 Memory usage grows / objects live forever (sub: perf) platform/iOS 🍎
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants