I am wondering if the code below will produce a leak: Class Parent
has a list of class Child
. Child
objects use PropertyChangedEventManager.AddHandler
to be notified of property changes in the aggregating Parent
object. If those aggregated instances never call RemoveHandler
, will it produce a leak?
I understand that the GC is based upon reachability, not reference counting, but clearly the PropertyChangedEventManager
is maintaining a list of handler subscribers somewhere. So how does that list get cleaned up if I don't call RemoveHandler
?
Something like this admittedly simplified version of what my code does:
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...
}
}