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

[BUG][Android] BindingContext never cleared on Android after #3151 was merged causing vm's to memory leak #3186

Open
skha83 opened this issue Jul 9, 2024 · 1 comment
Labels
bug Commercial Plus 🚀 Issue or Pull Request opened by a Commercial Plus license holder to verify

Comments

@skha83
Copy link

skha83 commented Jul 9, 2024

Description

Ever since the changes in #3151 was merged to fix #3113 the BindingContext of views are never cleared on Android, because the Unloaded event is never triggered.

Steps to Reproduce

This can be reproduced by launching the E2E demo on the main branch on an Android device.
Set a breakpoint here

DestroyChildren(view);
and here
visualElement.Unloaded -= VisualElementUnloaded;

Navigate around in the app opening sub pages and close them again.
An example could be:

  1. Go to Tabbed Page
  2. Navigate to Tab B
  3. Open View C page
  4. Navigate back to View B by pressing the back arrow in upper left corner.

The first breakpoint will get triggered correctly, but the 2nd breakpoint will never get triggered.

Because of this the ViewModels will never be garbage collected/Disposed. The Destroy method will of course be called if the ViewModel implements IDestructible. But if the ViewModel also implements IDisposable, the Dispose method will never be called. This happened before this change, whenever the BindingContext was cleared.

From the MAUI documentation it says that the Unloaded event "Occurs when an element is no longer connected to the main object tree." https://learn.microsoft.com/en-us/dotnet/api/microsoft.maui.controls.visualelement.unloaded?view=net-maui-8.0
For some reason this event is never triggered whenever you close a sub page or navigate to another page which is not part of a navigation stack. I'm not sure who is responsible for removing the page so that it is no longer connected to the main object tree and this event will get triggered.

Platform with bug

.NET MAUI

Affected platforms

Android

Did you find any workaround?

I haven't found a workaround.

Relevant log output

No response

@skha83 skha83 changed the title [BUG] BindingContext never cleared on Android after #3151 was merged [BUG][Android] BindingContext never cleared on Android after #3151 was merged causing vm's to memory leak Sep 3, 2024
@dansiegel dansiegel added the Commercial Plus 🚀 Issue or Pull Request opened by a Commercial Plus license holder label Oct 7, 2024
@brianlagunas
Copy link
Member

The work around would be to implement IDestructible and clear the binding context yourself.

We are having trouble in this area because of how MAUI works. Before we would clear the binding context automatically, but that would happen too soon, and you would see a visual artifact as the Page was being removed from the view.

Also, please use a memory monitoring tool to verify the objects are being rooted, and which object is rooting them causing the memory leak. The most common mistake people make when they think they see a memory leak is by watching the memory increase in the task manager. That is not an indication of a memory leak.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Commercial Plus 🚀 Issue or Pull Request opened by a Commercial Plus license holder to verify
Projects
None yet
Development

No branches or pull requests

3 participants