-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
[ios] fix memory leak with Page
+ Layout
#14108
Conversation
src/Controls/tests/DeviceTests/Elements/NavigationPage/NavigationPageTests.cs
Outdated
Show resolved
Hide resolved
internal Func<double, double, Size>? CrossPlatformMeasure { get; set; } | ||
internal Func<Rect, Size>? CrossPlatformArrange { get; set; } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I removed these, because you can just call View.CrossPlatformMeasure()
directly.
Otherwise we might need to unset them in places, like in 7d0af63.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I removed these, because you can just call
View.CrossPlatformMeasure()
directly.
The design intent here was for the handlers to be able to directly modify the xplat measure/arrange methods directly when necessary. It's not currently used on iOS (mostly for accidental historical reasons), but the other platforms do make use of it. The intent was to keep the designs symmetric on all the platforms.
(The main place you can see this at work in the Android and Windows ScrollViewHandlers.)
So I'd rather not remove these. Or, if there's a compelling memory-leak reason to remove them, we'll need to work out a way to inject custom xplat measure/arrange methods from the handlers.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The design intent here was for the handlers to be able to directly modify the xplat measure/arrange methods directly when necessary.
Is this design documented? So other people will know about it?
Or, if there's a compelling memory-leak reason to remove them, we'll need to work out a way to inject custom xplat measure/arrange methods from the handlers.
The leak appears to return if I put them back. It makes the shell & navigation DoNotLeak
tests fail. Will share a *.gcdump
file tomorrow.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this design documented? So other people will know about it?
Clearly not well enough, if at all. 🙂
Why is the |
Further fixes: dotnet#13520 7d0af63 did not appear to be a complete fix for dotnet#13520, as when I retested the customer sample, I found I needed to remove a `VerticalStackLayout` for the problem to go away. I could reproduce the issue in a test by doing: var page = new ContentPage { Title = "Page 2", Content = new VerticalStackLayout { new Label() } }; After lots of debugging, I found the underlying issue was that `MauiView` on iOS held a reference to the cross-platform `IView`. After changing the backing field for this to be a `WeakReference<IView>` the above problem appears to be solved. I also removed `LayoutView.CrossplatformArrange/Measure` as we can just use the `View` property directly instead.
cad3a96
to
e1648f2
Compare
The It may be that we are still leaking I believe there could be some core problem here that isn't understood. But I've not been able to reproduce a real issue here: https://github.com/jonathanpeppers/MemoryLeaksOniOS |
Here is the path to the leak with the changes reverted: I changed the You can unzip & open this in VS: my-dev-port_20230323_094815.zip |
I've been doing more testing and I think this is what was going on:
I made no. 3 a weak reference, and this appears to break the cycle and all of the above can go away... Note that cycles are only a problem for I just retested with the fix in place, and I don't see |
Ok, I have a minimal repro of a leak on iOS. The core issue was doing something like this: class MyViewSubclass : UIView
{
public UIView? Parent { get; set; }
public void Add(MyViewSubclass subview)
{
subview.Parent = this;
AddSubview(subview);
}
}
//...
var parent = new MyViewSubclass();
var view = new MyViewSubclass();
parent.Add(view); Appears to leak unless you do: view.Parent = null; |
@hartez do you have any other concerns here? |
Further fixes #13520
7d0af63 did not appear to be a complete fix for #13520, as when I retested the customer sample, I found I needed to remove a
VerticalStackLayout
for the problem to go away.I could reproduce the issue in a test by doing:
After lots of debugging, I found the underlying issue was that
MauiView
on iOS held a reference to the cross-platformIView
.After changing the backing field for this to be a
WeakReference<IView>
the above problem appears to be solved.I also removed
LayoutView.CrossplatformArrange/Measure
as we can just use theView
property directly instead.