I have the following situation.
A List<WeakReference<MyClass>>
, MyClass
has an event called MyEvent
. I iterate over the list, and for every alive reference, I might invoke the MyEvent
on it's target.
In other parts of my code, I keep live references to these objects. Let's say the process looks something like this:
var list = new List<WeakReference<MyClass>>();
var a = new MyClass();
a.MyEvent += delegate {
DoSomeStuff(a);
};
list.Add(new WeakReference<MyClass>(a));
Now let's say I have no further use of a
, and decide to dispose of it. So now, a = null
.
I have a periodic checks setup that clear the original list of dead references. What I'm wondering is, will this event handler I have hooked up keep the former a
instance of MyClass
alive, and in turn prevent me to clean it up from the list?
What if the event wasn't just a normal Action
, but instead an Action<MyClass>
that passed itself as the parameter to the handlers? In a way, that handler still has a reference to the original object, even though the handler might never get called.
This might be really obvious and I can think of similar situations where I'd know the answer but the fact that I'm hooking that function on the very object makes me suspicious. If it does keep it alive it breaks my whole system so I need to know.