You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
Navigate around in the app opening sub pages and close them again.
An example could be:
Go to Tabbed Page
Navigate to Tab B
Open View C page
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
The text was updated successfully, but these errors were encountered:
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
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.
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
Prism/src/Maui/Prism.Maui/Common/MvvmHelpers.cs
Line 62 in 90a701f
Prism/src/Maui/Prism.Maui/Common/MvvmHelpers.cs
Line 72 in 90a701f
Navigate around in the app opening sub pages and close them again.
An example could be:
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
The text was updated successfully, but these errors were encountered: