-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Fix memory leak in VisualElement.Shadow #13960
Conversation
src/Controls/src/Core/HandlerImpl/VisualElement/VisualElement.Impl.cs
Outdated
Show resolved
Hide resolved
/// General usage is to store this in a member variable and call Subscribe()/Unsubscribe() appropriately. | ||
/// Your class should have a finalizer that calls Unsubscribe() to prevent WeakNotifyCollectionChangedProxy objects from leaking. | ||
/// </summary> | ||
class WeakNotifyPropertyChangedProxy : WeakEventProxy<INotifyPropertyChanged, PropertyChangedEventHandler> |
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.
This is a generic proxy to manage the PropertyChanged event.
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.
This PR looks correct, if the tests pass.
So the only outstanding issue is what to do about Parent
. I don't know if we need to solve that before merging?
src/Controls/src/Core/HandlerImpl/VisualElement/VisualElement.Impl.cs
Outdated
Show resolved
Hide resolved
@mandel-macaque the reason to make a local here is in case two threads are in here at once. |
src/Controls/src/Core/HandlerImpl/VisualElement/VisualElement.Impl.cs
Outdated
Show resolved
Hide resolved
@jonathanpeppers That is valid point in a multithreaded case, BUT in that case the assignment would be an issue since the ??= operator will be expanded to a if (f != null) f = new(); Therefore we have two possible cases:
You can see how a ??= is the following: public static class Program
{
[System.Runtime.CompilerServices.Nullable(2)]
private static object _test;
[System.Runtime.CompilerServices.NullableContext(1)]
public static object Test()
{
if (_test == null)
{
_test = new object();
}
return _test;
}
} Two threads can be creating two diff _shadowObjets. So either for 1 or 2 the code is not correct. |
@jonathanpeppers @jsuarezruiz for reference: dotnet/docs#27613 BUT the using null conditional operators IS thread safe (the joys of the documentation): Null-conditional operators ?. and ?[] So be careful, some are thread-safe, but not all are thread-safe. Example: static object? _test = null;
public static object Test () {
_test ??= new object();
_test?.ToString ();
return _test;
} Generated:
|
if (shadow is not null) | ||
{ | ||
Shadow.Parent = this; | ||
Shadow.PropertyChanged += OnShadowChanged; | ||
var proxy = _shadowProxy ??= new(); | ||
proxy.Subscribe(shadow, (sender, e) => OnPropertyChanged(nameof(Shadow))); |
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.
As seen in: #13997
I think you can change this to:
_shadowHandler ??= (sender, e) => OnPropertyChanged(nameof(Shadow));
_shadowProxy ??= new();
proxy.Subscribe(shadow, _shadowHandler);
Where _shadowHandler
is a member field to hold onto the EventHandler
and make this test pass: https://github.com/dotnet/maui/pull/13960/files#r1139276236
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.
Ok, just so that people get why we need the extra local var, we are not trying to fix a thread race condition, but a memory race condition in which the GC kicks ins that is why the code is confusing. There are not thread race conditions because everything should be happening in the main/ui thread. Is just that the fix is to use a local var to keep the ref until we don't longer need it.
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.
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.
This looks good now, hopefully CI is green. 👍
Description of Change
This is very much to 58a42e5 and 61fbb00
I used the same technique in 58a42e5, and added a unit test to validate the changes.
NOTE: It's a Draft until we review how to properly propagate the Parent to the Shadow (and others).