Skip to content

[ios] fix memory leak in Picker#16685

Merged
jfversluis merged 1 commit intodotnet:mainfrom
jonathanpeppers:PickerOhNo
Aug 15, 2023
Merged

[ios] fix memory leak in Picker#16685
jfversluis merged 1 commit intodotnet:mainfrom
jonathanpeppers:PickerOhNo

Conversation

@jonathanpeppers
Copy link
Copy Markdown
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 perf/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
@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 perf/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