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 SearchBar #16383

Merged
merged 5 commits into from
Feb 27, 2024
Merged

Commits on Jan 8, 2024

  1. [ios] fix memory leak in SearchBar

    Context: dotnet#16346
    
    This addresses the memory leak discovered by:
    
        src/Core/src/Platform/iOS/MauiSearchBar.cs(36,63): error MA0001: Event 'TextSetOrChanged' could cause memory leaks in an NSObject subclass. Remove the event or add the [UnconditionalSuppressMessage("Memory", "MA0001")] attribute with a justification as to why the event will not leak.
        src/Core/src/Platform/iOS/MauiSearchBar.cs(54,32): error MA0001: Event 'OnMovedToWindow' could cause memory leaks in an NSObject subclass. Remove the event or add the [UnconditionalSuppressMessage("Memory", "MA0001")] attribute with a justification as to why the event will not leak.
        src/Core/src/Platform/iOS/MauiSearchBar.cs(55,32): error MA0001: Event 'EditingChanged' could cause memory leaks in an NSObject subclass. Remove the event or add the [UnconditionalSuppressMessage("Memory", "MA0001")] attribute with a justification as to why the event will not leak.
        src/Core/src/Platform/iOS/MauiSearchBar.cs(70,31): error MA0003: Subscribing to events with instance method 'OnEditingChanged' could cause memory leaks in an NSObject subclass. Remove the subscription or convert the method to a static method.
    
    I could reproduce a leak in a test such as:
    
        await InvokeOnMainThreadAsync(() =>
        {
            var layout = new Grid();
            var searchBar = new SearchBar();
            layout.Add(searchBar);
            var handler = CreateHandler<LayoutHandler>(layout);
            viewReference = new WeakReference(searchBar);
            handlerReference = new WeakReference(searchBar.Handler);
            platformViewReference = new WeakReference(searchBar.Handler.PlatformView);
        });
    
        await AssertionExtensions.WaitForGC(viewReference, handlerReference, platformViewReference);
        Assert.False(viewReference.IsAlive, "SearchBar should not be alive!");
        Assert.False(handlerReference.IsAlive, "Handler should not be alive!");
        Assert.False(platformViewReference.IsAlive, "PlatformView should not be alive!");
    
    Solved the issues by making a non-NSObject `MauiSearchBarProxy` class.
    
    I found & fixed a couple related issues:
    
    * `SearchBarExtensions.GetSearchTextField()` would have always thrown
      `StackOverlowException` if iOS was less than 13? It just called itself?!?
    
    * Removed `MauiSearchBar.EditingChanged`, as we could subscribe to the
      event from the `UITextField` directly instead.
    jonathanpeppers committed Jan 8, 2024
    Configuration menu
    Copy the full SHA
    b3eb860 View commit details
    Browse the repository at this point in the history
  2. Configuration menu
    Copy the full SHA
    f8219bf View commit details
    Browse the repository at this point in the history
  3. Code cleanup

    jonathanpeppers committed Jan 8, 2024
    Configuration menu
    Copy the full SHA
    639d42c View commit details
    Browse the repository at this point in the history

Commits on Feb 26, 2024

  1. Configuration menu
    Copy the full SHA
    eda250a View commit details
    Browse the repository at this point in the history
  2. Configuration menu
    Copy the full SHA
    f887e99 View commit details
    Browse the repository at this point in the history