Maui icon indicating copy to clipboard operation
Maui copied to clipboard

[BUG] Popup causes the current page to leak on iOS

Open albyrock87 opened this issue 1 year ago • 1 comments

Is there an existing issue for this?

  • [X] I have searched the existing issues

Did you read the "Reporting a bug" section on Contributing file?

  • [X] I have read the "Reporting a bug" section on Contributing file: https://github.com/CommunityToolkit/Maui/blob/main/CONTRIBUTING.md#reporting-a-bug

Current Behavior

Popup retains a reference to current page after close.

Expected Behavior

The Popup should not retain a reference after close.

Steps To Reproduce

  • show a popup using IPopupService
  • pop the current page (shell, or navigation page doesn't matter)
  • popped page is still in memory because Popup retains a reference to it (use a WeakReference to verify that)

Link to public reproduction project repository

Environment

- .NET MAUI CommunityToolkit: 7.0.1
- OS: iOS 17
- .NET MAUI: 8.0.6

Anything else?

Here's an explanation of where the problem is, and a proposal on how to fix.

On iOS we're creating a ContentPage to hold the VirtualView. This ContentPage references the current page via Parent property. https://github.com/CommunityToolkit/Maui/blob/666f0e97ced203c8db310b1cb772c9dcb6f37f7c/src/CommunityToolkit.Maui/HandlerImplementation/Popup/Popup.macios.cs#L31

We need to clear this on close, otherwise there'll be memory leak. Add an OnClose handler which sets the ContentPage.Parent property to null.

I tested this using the debugger VirtualView.Parent.Parent = null and it solves the memory leak.

albyrock87 avatar Feb 03 '24 13:02 albyrock87

Any news on this issue?

GUELIANEBelkacem avatar Jul 02 '24 03:07 GUELIANEBelkacem

@albyrock87 @GUELIANEBelkacem could you please give us a hand by testing the latest version 9.0.2, and using .NET MAUI 8.0.70, and see if it fixes the memory leak? Also, if you could provide a sample repository app showing the memory leak will be fantastic, please.

thanks in advance

cc: @pictos

vhugogarcia avatar Jul 23 '24 18:07 vhugogarcia

@vhugogarcia I can confirm latest version fixes the issue.

My library Nalu provides a leak detection mechanism which shows an alert if a page leaked after being popped.

You can use this to test also other components if you need.

See the recording demonstrating the leak of previous versions (which is now solved with 9.0.2):

Simulator Screen Recording - iPhone 15 Pro Max - 2024-07-27 at 18 23 07

albyrock87 avatar Jul 27 '24 16:07 albyrock87

Thanks @albyrock87 for the feedback and sharing the results. 😃

vhugogarcia avatar Jul 27 '24 18:07 vhugogarcia