PropertyChangedEventManager - 调用 RemoveHandler 失败会导致泄漏吗?
PropertyChangedEventManager - Will failing to call RemoveHandler cause a leak?
我想知道下面的代码是否会产生泄漏:Class Parent
有一个 class Child
的列表。 Child
对象使用 PropertyChangedEventManager.AddHandler
来通知聚合 Parent
对象中的 属性 变化。如果这些聚合实例从不调用 RemoveHandler
,是否会产生泄漏?
我明白 ,但显然 PropertyChangedEventManager
正在某处维护一个处理程序订阅者列表。那么,如果我不调用 RemoveHandler
,该列表如何清理?
像这样的东西是我的代码所做的公认的简化版本:
public class Parent : INotifyPropertyChanged
{
public Parent()
{
_children = new List<Child>();
_children.Add(new Child(this));
}
public event PropertyChangedEventHandler PropertyChanged;
public bool Value
{
get => _value;
set
{
_value = value;
PropertyChanged?.Invoke(new PropertyChangedEventArgs("Value"));
}
}
private List<Child> _children
}
public class Child
{
public Child(Parent parent)
{
// Will failing to undo this with RemoveHandler cause a leak?
PropertyChangedEventManager.AddHandler(r, OnValueChange, "Value");
}
void OnValueChanged(object sender, PropertyChangedEventArgs e)
{
// Do something...
}
}
答案是否定的
正如 PropertyChangedEventManager
(WeakEventManager
) 的基类型名称所暗示的那样,所有注册到 PropertyChangedEventHandler 的处理程序将保留为 Weak References
如您所知,
A weak reference permits the garbage collector to collect the object
while still allowing the application to access the object. A weak
reference is valid only during the indeterminate amount of time until
the object is collected when no strong references exist
因此我们可以确定 class 引用(事件处理程序的声明实例)不会泄漏。
RemoveHandler
从事件中分离处理程序。它从 table 中删除一个条目,属性 已更改的事件管理器维护以跟踪事件及其处理程序。
事件管理器还有一个清除过程,用于清除 GC 收集到的弱引用目标的任何条目。
所有这些都是从逆向工程开始的:https://referencesource.microsoft.com/#windowsbase/Base/System/ComponentModel/PropertyChangedEventManager.cs
我想知道下面的代码是否会产生泄漏:Class Parent
有一个 class Child
的列表。 Child
对象使用 PropertyChangedEventManager.AddHandler
来通知聚合 Parent
对象中的 属性 变化。如果这些聚合实例从不调用 RemoveHandler
,是否会产生泄漏?
我明白 PropertyChangedEventManager
正在某处维护一个处理程序订阅者列表。那么,如果我不调用 RemoveHandler
,该列表如何清理?
像这样的东西是我的代码所做的公认的简化版本:
public class Parent : INotifyPropertyChanged
{
public Parent()
{
_children = new List<Child>();
_children.Add(new Child(this));
}
public event PropertyChangedEventHandler PropertyChanged;
public bool Value
{
get => _value;
set
{
_value = value;
PropertyChanged?.Invoke(new PropertyChangedEventArgs("Value"));
}
}
private List<Child> _children
}
public class Child
{
public Child(Parent parent)
{
// Will failing to undo this with RemoveHandler cause a leak?
PropertyChangedEventManager.AddHandler(r, OnValueChange, "Value");
}
void OnValueChanged(object sender, PropertyChangedEventArgs e)
{
// Do something...
}
}
答案是否定的
正如 PropertyChangedEventManager
(WeakEventManager
) 的基类型名称所暗示的那样,所有注册到 PropertyChangedEventHandler 的处理程序将保留为 Weak References
如您所知,
A weak reference permits the garbage collector to collect the object while still allowing the application to access the object. A weak reference is valid only during the indeterminate amount of time until the object is collected when no strong references exist
因此我们可以确定 class 引用(事件处理程序的声明实例)不会泄漏。
RemoveHandler
从事件中分离处理程序。它从 table 中删除一个条目,属性 已更改的事件管理器维护以跟踪事件及其处理程序。
事件管理器还有一个清除过程,用于清除 GC 收集到的弱引用目标的任何条目。
所有这些都是从逆向工程开始的:https://referencesource.microsoft.com/#windowsbase/Base/System/ComponentModel/PropertyChangedEventManager.cs