i read the answer of the question Do event handlers stop garbage collection from occuring?, but what happens when the publisher is the target ?
To be more specific, i am using MVVM design for a WPF app. Model-View classes raise a NotifyPropertyChanged on every change. In some classes, i need to call a method when something is modified.
I would like to do this:
this.PropertyChanged += this.MyHandler;
Will this instances be destroyed by the GC ?
The GC looks and sees if any references to the object are currently rooted in the application. It is smart enough to handle circular references like the one above.
In addition, it is smart enough to handle the case where you have two objects, A, and B, and:
If A and B both go out of scope, the GC is smart enough to find and clean both of these objects, even though they subscribe to each other. However, if a separate object (in use) refers to either, it will prevent both from being collected.
This is one of the main advantages to a true GC solution, when compared to reference counting solutions. Reference counting will fail to collect this, but the .NET gc will handle it perfectly.