473,406 Members | 2,956 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Disposing delegates of an event

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
Nov 15 '05 #1
4 10891
"Greg Patrick" <pr*****@private.com> wrote in message
news:B7neb.5191$f7.343045@localhost...
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...
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

Nov 15 '05 #2
Greg Patrick <pr*****@private.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #3
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
Nov 15 '05 #4
100
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

"Greg Patrick" <pr*****@private.com> wrote in message
news:B7neb.5191$f7.343045@localhost...
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

Nov 15 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Ian Richardson | last post by:
I'm writing some code which does one thing when onreadystatechange occurs, e.g. handle.onreadystatechange = function() { blah(handle,other_params) }; ....but sometimes I need to add another,...
1
by: bigsincebirth | last post by:
HAPPY THANKSGIVING EVERYONE! I'd be really THANKFUL for any help. :wink: I'm a java programmer who has recently been assigned to a c# project. I'm having trouble communicating between my forms. ...
4
by: LP | last post by:
Hello! I am still transitioning from VB.NET to C#. I undertand the basic concepts of Delegates, more so of Events and somewhat understand AsyncCallback methods. But I need some clarification on...
4
by: DKode | last post by:
Hello, I have a question about delegates and events I have a basic understanding of each, but in the apps I'm building I'm never needing to use these tools. I'm wondering when and where you...
5
by: Paul Bromley | last post by:
Can someone give me a very simple example on how to do this? As an example I have a commaned button in a user control. Once this user control is placed on a form I want to be able to respond in the...
0
by: Gary Shell | last post by:
I am experiencing some strange behavior between a UserControl's validating event and a treeview control. Initially, I thought it was related to an issue in the Knowledgebase article 810852...
11
by: Snowbleach | last post by:
Hello, I am having trouble with the following console application. I am using delegates/event to make a clock raise an alarm when a certain amount of time has been reached. The following compiles,...
1
by: =?Utf-8?B?bXIgcGVhbnV0?= | last post by:
I have a application which involves creating instances of a "Form2" class. Form2 has an event which I handle in Form1. Instances of Form2 have names like "Matrix_A", "Matrix_B", etc. To create them...
7
by: Siegfried Heintze | last post by:
I'm studying the book "Microsoft Visual Basic.NET Language Reference" and I would like some clarify the difference between events and delegates. On page 156 I see a WinForms example of timer that...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.