Skip to content

[ios] fix memory leak in Editor#16348

Merged
rmarinho merged 1 commit intodotnet:mainfrom
jonathanpeppers:iOSEditorLeaks
Jul 26, 2023
Merged

[ios] fix memory leak in Editor#16348
rmarinho merged 1 commit intodotnet:mainfrom
jonathanpeppers:iOSEditorLeaks

Conversation

@jonathanpeppers
Copy link
Copy Markdown
Member

Context: #16346

This addresses the memory leak discovered by:

src/Core/src/Platform/iOS/MauiTextView.cs(37,30): 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/MauiTextView.cs(12,20): error MA0002: Member '_placeholderLabel' 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 a test such as:

await InvokeOnMainThreadAsync(() =>
{
    var layout = new Grid();
    var editor = new Editor();
    layout.Add(editor);
    var handler = CreateHandler<LayoutHandler>(layout);
    viewReference = new WeakReference(editor);
    handlerReference = new WeakReference(editor.Handler);
    platformViewReference = new WeakReference(editor.Handler.PlatformView);
});

await AssertionExtensions.WaitForGC(viewReference, handlerReference, platformViewReference);
Assert.False(viewReference.IsAlive, "Editor should not be alive!");
Assert.False(handlerReference.IsAlive, "Handler should not be alive!");
Assert.False(platformViewReference.IsAlive, "PlatformView should not be alive!");

I will create a similar PR for Entry as well.

@jonathanpeppers jonathanpeppers added the perf/memory-leak 💦 Memory usage grows / objects live forever (sub: perf) label Jul 25, 2023
Context: dotnet#16346

This addresses the memory leak discovered by:

    src/Core/src/Platform/iOS/MauiTextView.cs(37,30): 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/MauiTextView.cs(12,20): error MA0002: Member '_placeholderLabel' 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 a test such as:

    await InvokeOnMainThreadAsync(() =>
    {
        var layout = new Grid();
        var editor = new Editor();
        layout.Add(editor);
        var handler = CreateHandler<LayoutHandler>(layout);
        viewReference = new WeakReference(editor);
        handlerReference = new WeakReference(editor.Handler);
        platformViewReference = new WeakReference(editor.Handler.PlatformView);
    });

    await AssertionExtensions.WaitForGC(viewReference, handlerReference, platformViewReference);
    Assert.False(viewReference.IsAlive, "Editor should not be alive!");
    Assert.False(handlerReference.IsAlive, "Handler should not be alive!");
    Assert.False(platformViewReference.IsAlive, "PlatformView should not be alive!");

I will create a similar PR for `Entry` as well.
@jonathanpeppers jonathanpeppers marked this pull request as ready for review July 26, 2023 01:24
@rmarinho rmarinho merged commit 13cfac6 into dotnet:main Jul 26, 2023
@jonathanpeppers jonathanpeppers deleted the iOSEditorLeaks branch July 26, 2023 13:43
@github-actions github-actions bot locked and limited conversation to collaborators Dec 8, 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

area-controls-editor Editor 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.

4 participants