Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When is PropertyChangedEventManager created and when is it attached?

To make a very long story short… This is in a large WPF project

I have a class “Patient” that implements INotifyPropertyChanged. When I dispose this class I am checking that the PropertyChangedEventHandler is null and if not run it to a ListenerDetector class that logs the listeners so we can track them down and clean the leaks. Patient does have some properties that are bound to WPF elements as well as other objects the use its PropertyChanged to monitor changes.

After disposing everything else, my log result is as follows:

Log: Patient still has the following listener(s) attached: - System.ComponentModel.PropertyChangedEventManager

Question: When is the PropertyChangedEventManager created?

  • When Patient is created
  • When WPF binds to Patient property
  • Some other point.

If Patient.PropertyChangedEventHandler is set to null and therefore disconnected from the PropertyChangedEventManager is there any way to recreate the Manager and have it listen to the Patient.PropertyChangedEventHandler again? Please don’t ask why this would happen, that is a point of tension here :-(

like image 410
Paul Matovich Avatar asked Oct 25 '11 18:10

Paul Matovich


1 Answers

The PropertyChangedEventManager is created by WPF in order to support binding to any class that implements INotifyPropertyChanged. It'll be created and used as soon as you bind to any class that implements INotifyPropertyChanged.

That being said, its an implementation of the WeakEventManager pattern. While it's still showing a listener attached, realize that this listener is attached using the Weak Event Pattern. This will go away as soon as a full garbage collection occurs, as it's using weak references to hold the subscription. As such, this particular subscription shouldn't be the cause of a (long term) memory leak.

like image 130
Reed Copsey Avatar answered Nov 15 '22 22:11

Reed Copsey