Chris DiPierro <cd******@mi-corporation.com> wrote:
Let's say I do the following:
Object A lives "forever" and periodically creates instances of Object B.
Object B triggers events, that Object A registers for immediately upon
the creation of Object B.
Object B is later dereferenced.
Do my handlers to the now dereferences Object B ever get garbage
collected? Or should I explicitly unregister for the events before I
dereference Object B?
That depends really on what you mean by the second part. If you mean:
a.SomeEvent += new EventHandler (b.SomeMethod);
then you need to remove that delegate from the event before b can be
garbage collected.
If you mean:
b.SomeEvent += new EventHandler (a.SomeMethod);
then you don't need to do anything.
Basically, the event has a reference to the target of the delegate, but
not the other way round.
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too