Disposing delegates of an event

  • Thread starter Thread starter Greg Patrick
  • Start date Start date
G

Greg Patrick

Let's say I have a Form and I create a delegate (MyDelegate) and an event
(SomethingHappened) for that form.

Now other classes add their delegate to the event, e.g.
theForm.SomethingHappened += new MyDelegate(this.myMethod);

When I Close the form, how do I properly dispose of the delegates of the
event?

Does each subcriber need to do a -= ?

Or can I just put this.SomethingHappened = null in the Dispose(bool) of the
Form?

I want to make sure the subscribers don't still have a reference that would
prevent garbage collection of the form...

Greg Patrick
 
Greg Patrick said:
Let's say I have a Form and I create a delegate (MyDelegate) and an event
(SomethingHappened) for that form.

Now other classes add their delegate to the event, e.g.
theForm.SomethingHappened += new MyDelegate(this.myMethod);

When I Close the form, how do I properly dispose of the delegates of the
event?

Does each subcriber need to do a -= ?

Or can I just put this.SomethingHappened = null in the Dispose(bool) of the

I want to make sure the subscribers don't still have a reference that would
prevent garbage collection of the form...

As far as I know, you're right in thinking that the reference will be kept
alive until you unhook the delegate. I'd look for some event that that your
subscriber can listen for to indicate it is no longer interested in
receiving notifications from the publisher. For example, you could unhook
after receiving the closed event, or possibly even the HandleDestroyed
event?

I was fighting with this a few months ago, and ended up getting hold of a
heap profiler to analyse any allocations that were not being collected. The
free profiler on GotDotNet may help you with this.

HTH

Tobin
 
Greg Patrick said:
Let's say I have a Form and I create a delegate (MyDelegate) and an event
(SomethingHappened) for that form.

Now other classes add their delegate to the event, e.g.
theForm.SomethingHappened += new MyDelegate(this.myMethod);

When I Close the form, how do I properly dispose of the delegates of the
event?

Does each subcriber need to do a -= ?

Or can I just put this.SomethingHappened = null in the Dispose(bool) of the
Form?

There's no need to do either, so long as nothing else has a reference
to the form - so long as the form itself is garbage collected, its
eventhandlers will be too. The time when you need to manually unhook
them is if the form instance itself would stick around, but you didn't
want the eventhandlers to.
 
Just a note: I assumed that you were subscribing to events from another
form/control/class?
theForm.SomethingHappened += new MyDelegate(this.myMethod);

Therefore, I think you have to explicitly remove the subscriber from the
invocation list using the -= operator.

Tobin
 
Hi Greg,
It depends of what do you mean by closing the form. If you remove all
references to the form object you shouldn't bother for removing the events'
subscribers. As long as events are not *root* references they will not keep
the subscribers alive. If you, however, dispose the form and keep references
to the form Dispose method will destroy underlaying native window, but the
form object itself will be considered as alive in the managed heap. In the
latter case all subscribers will be considered as referenced by the GC.

HTH
B\rgds
100
 
Back
Top