Skip to content

[ios] fix memory leak in SwipeView#16532

Merged
jonathanpeppers merged 1 commit intodotnet:mainfrom
jonathanpeppers:SwipeViewOhNo
Aug 4, 2023
Merged

[ios] fix memory leak in SwipeView#16532
jonathanpeppers merged 1 commit intodotnet:mainfrom
jonathanpeppers:SwipeViewOhNo

Conversation

@jonathanpeppers
Copy link
Copy Markdown
Member

Context: #16346

This addresses the memory leak discovered by:

src/Core/src/Platform/iOS/MauiSwipeView.cs(20,10): error MA0002: Member '_contentView' 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.
src/Core/src/Platform/iOS/MauiSwipeView.cs(21,15): error MA0002: Member '_actionView' 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.
src/Core/src/Platform/iOS/MauiSwipeView.cs(18,26): error MA0002: Member '_tapGestureRecognizer' 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.
src/Core/src/Platform/iOS/MauiSwipeView.cs(19,26): error MA0002: Member '_panGestureRecognizer' 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(SwipeView))]
public async Task HandlerDoesNotLeak(Type type)

Solved the problem by:

  • Remove the backing field for MauiSwipeView.Element. We can use CrossPlatformLayout, which is already safe, and just cast to ISwipeView instead.

  • Create a SwipeRecognizerProxy type for handling callbacks from gesture recognizers.

Other cleanup:

  • Marked fields readonly as Roslyn suggested.

  • Fixed a place .Count() > 0 was used where Roslyn suggested to use .Any() instead.

  • GetIsVisible() can also be static to avoid a closure on this:

Any(s => this.GetIsVisible(s))

Context: dotnet#16346

This addresses the memory leak discovered by:

    src/Core/src/Platform/iOS/MauiSwipeView.cs(20,10): error MA0002: Member '_contentView' 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.
    src/Core/src/Platform/iOS/MauiSwipeView.cs(21,15): error MA0002: Member '_actionView' 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.
    src/Core/src/Platform/iOS/MauiSwipeView.cs(18,26): error MA0002: Member '_tapGestureRecognizer' 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.
    src/Core/src/Platform/iOS/MauiSwipeView.cs(19,26): error MA0002: Member '_panGestureRecognizer' 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(SwipeView))]
    public async Task HandlerDoesNotLeak(Type type)

Solved the problem by:

* Remove the backing field for `MauiSwipeView.Element`. We can use
  `CrossPlatformLayout`, which is already safe, and just cast to
  `ISwipeView` instead.

* Create a `SwipeRecognizerProxy` type for handling callbacks from
  gesture recognizers.

Other cleanup:

* Marked fields `readonly` as Roslyn suggested.

* Fixed a place `.Count() > 0` was used where Roslyn suggested to use
  `.Any()` instead.

* `GetIsVisible()` can also be `static` to avoid a closure on `this`:

    Any(s => this.GetIsVisible(s))
@jonathanpeppers jonathanpeppers added platform/ios perf/memory-leak 💦 Memory usage grows / objects live forever (sub: perf) labels Aug 3, 2023
@Eilon Eilon added the legacy-area-perf Startup / Runtime performance label Aug 3, 2023
@jonathanpeppers jonathanpeppers marked this pull request as ready for review August 4, 2023 02:42
@jsuarezruiz
Copy link
Copy Markdown
Contributor

/azp run

@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines successfully started running 3 pipeline(s).

@jonathanpeppers jonathanpeppers merged commit 45a5815 into dotnet:main Aug 4, 2023
@jonathanpeppers jonathanpeppers deleted the SwipeViewOhNo branch August 4, 2023 17:24
@github-actions github-actions bot locked and limited conversation to collaborators Dec 7, 2023
@Eilon Eilon added perf/general The issue affects performance (runtime speed, memory usage, startup time, etc.) (sub: perf) area-controls-swipeview SwipeView and removed legacy-area-perf Startup / Runtime performance labels May 10, 2024
@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

area-controls-swipeview SwipeView fixed-in-8.0.0-rc.1.9171 Look for this fix in 8.0.0-rc.1.9171 perf/general The issue affects performance (runtime speed, memory usage, startup time, etc.) (sub: perf) 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.

4 participants