memory-analyzers icon indicating copy to clipboard operation
memory-analyzers copied to clipboard

Consider warning about `IPlatformViewHandler`

Open jonathanpeppers opened this issue 2 years ago • 2 comments

This problem came up with MAUI's Frame class, as FrameRenderer is not an NSObject but somehow produced a cycle that I could cause problems with at runtime.

Examples:

  • https://github.com/dotnet/maui/pull/18552
  • https://github.com/dotnet/maui/pull/18602
  • https://github.com/dotnet/maui/pull/18663
  • https://github.com/dotnet/maui/pull/18681

jonathanpeppers avatar Nov 06 '23 21:11 jonathanpeppers

Ok, I think I see the pattern that isn't currently caught by the analyzer:

class Foo
{
    UIButton bar = new();

    public Foo()
    {
        bar.TouchUpInside += OnTouch;
    }

    void OnTouch(object sender, EventArgs e) { }
}

Foo is a non-NSObject, but causes a cycle: Foo -> UIButton -> Foo.

I'll need to test in isolation to verify this leaks every time. It definitely leaks if Foo was a .NET MAUI handler class.

No idea how the analyzer could check for this yet... I guess warn on any event? That would warn on all usage of TouchUpInside!

jonathanpeppers avatar Nov 07 '23 23:11 jonathanpeppers

Ok, the simple example above, Foo, does not appear to leak in isolation:

https://github.com/jonathanpeppers/MemoryLeaksOniOS/compare/UIButtonEvents

So, this must only happen with MAUI IPlatformViewHandler types? There must be something that causes a cycle there.

jonathanpeppers avatar Nov 08 '23 21:11 jonathanpeppers