473,614 Members | 2,342 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Event Handler Destruction

When I, let's say I'm a form, register myself for an event, I use a syntax
such as:

Class1.EventNam e += new EventNameHandle r(MyFormsEventH andler);

Somewhere in global scope, I have defined the delegate EventNameHandle r:

public delegate void EventNameHandle r(object source, Eventargs e);

In Class1 I have:

public static event EventNameHandle r EventName;

I assume that the event EventName keeps track of all of the EventNameHandle r
delegates that are register with it. But who unregisters the delegate when
my form is destroyed? Can someone please explain?

Regards,
David Rogers
Nov 15 '05 #1
6 2066

"David Rogers" <dr*****@NOSPAM .fhcrc.org> wrote in message
news:e7******** ******@tk2msftn gp13.phx.gbl...
I assume that the event EventName keeps track of all of the EventNameHandle r delegates that are register with it. But who unregisters the delegate when
my form is destroyed? Can someone please explain?
I wondered about this, and used a heap profiler (.NETProfiler?) to
investigate what was going on. Basically, my forms were not dying until I
explicitly unregistered the delegates!! If you get hold of a heap profiler
(http://www.scitech.se/memprofiler/) you can investigate this yourself. I
supppose it kinda makes sense though, the forms are being referenced by the
delegates (by way of the callback), so the form shouldn't be garbage
collected until there is nothing in memory referencing it. I can't remember
if this is still the case even the delegate is set up between the form and a
control it contains?

Tobin
Regards,
David Rogers

Nov 15 '05 #2
Just to add, if want to unregister the delegate I find the best places for
adding and removing it are:

Control.OnHandl eCreated

and

Control.OnHandl eDestroyed

The Form class derives from Control so you can override them to do your
registering / unregistering.

Regards
Lee

"Jon Skeet" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@news.microsof t.com...
David Rogers <dr*****@NOSPAM .fhcrc.org> wrote:
When I, let's say I'm a form, register myself for an event, I use a syntax such as:

Class1.EventNam e += new EventNameHandle r(MyFormsEventH andler);

Somewhere in global scope, I have defined the delegate EventNameHandle r:

public delegate void EventNameHandle r(object source, Eventargs e);

In Class1 I have:

public static event EventNameHandle r EventName;

I assume that the event EventName keeps track of all of the EventNameHandle r delegates that are register with it. But who unregisters the delegate when my form is destroyed? Can someone please explain?


Unless you're doing it yourself, I don't believe anything is
unregistering the event. Your form instance will stick around...

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too

Nov 15 '05 #3
David Rogers <dr*****@NOSPAM .fhcrc.org> wrote:
Related question: Does the framework take care of the event handlers that
are added to controls by the designer? If not, does this mean that my child
form's button click handler is going to cause that form instance to remain
in memory even after the form is gone?

If that is the case, there must be 22 million programs out there that are
leaking memory like a sieve!


Not necessarily - it's only a memory leak if you're adding the delegate
to event *outside* the form itself. Normal forms where, say, clicking a
button triggers a label to change, don't have a memory leak - the whole
lot becomes eligible for garbage collection at the same time.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 15 '05 #4
David,
I do not believe in this instance its a problem, as the form has a reference
to the child & the child has a reference to the form. (circular references).

When you get ride of the form only that form references the children so
every thing should go away.

If your other example, form1 had a reference to form2, you had a reference
to form1, which was keeping every thing alive.

Hope this helps
Jay

"David Rogers" <dr*****@NOSPAM .fhcrc.org> wrote in message
news:OR******** *****@TK2MSFTNG P11.phx.gbl...
Related question: Does the framework take care of the event handlers that
are added to controls by the designer? If not, does this mean that my child form's button click handler is going to cause that form instance to remain
in memory even after the form is gone?

If that is the case, there must be 22 million programs out there that are
leaking memory like a sieve!

David

"Lee Alexander" <lee@No_Spam_Pl ease_Digita.com > wrote in message
news:e$******** *******@TK2MSFT NGP11.phx.gbl.. .
Just to add, if want to unregister the delegate I find the best places for
adding and removing it are:

Control.OnHandl eCreated

and

Control.OnHandl eDestroyed

The Form class derives from Control so you can override them to do your
registering / unregistering.

Regards
Lee

"Jon Skeet" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@news.microsof t.com...
David Rogers <dr*****@NOSPAM .fhcrc.org> wrote:
> When I, let's say I'm a form, register myself for an event, I use a

syntax
> such as:
>
> Class1.EventNam e += new EventNameHandle r(MyFormsEventH andler);
>
> Somewhere in global scope, I have defined the delegate EventNameHandle r: >
> public delegate void EventNameHandle r(object source, Eventargs e); >
> In Class1 I have:
>
> public static event EventNameHandle r EventName;
>
> I assume that the event EventName keeps track of all of the

EventNameHandle r
> delegates that are register with it. But who unregisters the

delegate when
> my form is destroyed? Can someone please explain?

Unless you're doing it yourself, I don't believe anything is
unregistering the event. Your form instance will stick around...

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too



Nov 15 '05 #5

"Jon Skeet" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@news.microsof t.com...
David Rogers <dr*****@NOSPAM .fhcrc.org> wrote:
Related question: Does the framework take care of the event handlers that are added to controls by the designer? If not, does this mean that my child form's button click handler is going to cause that form instance to remain in memory even after the form is gone?

If that is the case, there must be 22 million programs out there that are leaking memory like a sieve!
Not necessarily - it's only a memory leak if you're adding the delegate
to event *outside* the form itself. Normal forms where, say, clicking a
button triggers a label to change, don't have a memory leak - the whole
lot becomes eligible for garbage collection at the same time.


[Kind of related comments!...]
That does make sense actually - and saves us a little effort. Still a pain
for me becuase I use a kind of MVC architecture where forms subscribe to
events in 'back end' classes. So, I always have to remember to explicitly
code the unsubscribe when the form is destroyed. Thinking about it, would it
be possible to create a deligate class that could respond to the subscribers
Disposed event? When this is fired, it could remove the object from it's
list of recipients? So then calling Form.Dispose() would automatically
unsubscribe it from any delegates it's referenced by? Does anyone know if
this is possible? I don't even know if you can create custom delegates
though!? Just thinking out loud...

Tobin
--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too

Nov 15 '05 #6
Arghhhh! Of course not, the events themselves are destroyed with the form,
so there is no need to unregister the delegate instances that are registered
with those events...

Cheers!

"Jon Skeet" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@news.microsof t.com...
David Rogers <dr*****@NOSPAM .fhcrc.org> wrote:
Related question: Does the framework take care of the event handlers that are added to controls by the designer? If not, does this mean that my child form's button click handler is going to cause that form instance to remain in memory even after the form is gone?

If that is the case, there must be 22 million programs out there that are leaking memory like a sieve!


Not necessarily - it's only a memory leak if you're adding the delegate
to event *outside* the form itself. Normal forms where, say, clicking a
button triggers a label to change, don't have a memory leak - the whole
lot becomes eligible for garbage collection at the same time.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too

Nov 15 '05 #7

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

Similar topics

10
3577
by: tony kulik | last post by:
This code works fine in ie and opera but not at all in Mozilla. Anybody got a clue as to how to get it right? <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <script language="JavaScript" type="text/javascript"> function show(that) { if (box.style.visibility=='hidden') { that.style.visibility = 'visible'}; }
18
2874
by: Christopher W. Douglas | last post by:
I am writing a VB.NET application in Visual Studio 2003. I have written a method that handles several events, such as closing a form and changing the visible status of a form. I have some code that applies to all these events, but I need to have specific code execute when the form closes. The properties for this method are sender (the originator) and e (event arguments). I know how to get typeof (sender) to determine what form or...
8
6083
by: Mark | last post by:
Hi, I'm looking for some ideas on how to build a very simple Event processing framework in my C++ app. Here is a quick background ... I'm building a multithreaded app in C++ (on Linux) that uses message queues to pass pointers to Events between threads. In my app there are simple events that can be defined using an enum (for example an event called NETWORK_TIMEOUT) and more complex events that contain data (for example an event called...
1
2502
by: Kenneth Baltrinic | last post by:
I have a custom collection class that raises OnAdd and OnRemove events. It is used by another class (Class A) as a container for the class's children. Thus in class A's constructor, I instantiate an instance of the collection class and register event handlers for the two events. For a give instance of Class A, when is the appropriate time to release these handlers prior to the instances destruction? --Ken Baltrinic
6
1870
by: vbMark | last post by:
If I have a control, for example a CheckedListBox, how do I add and event to code, for example that a box has been checked by the user? Thanks
13
3496
by: Charles Law | last post by:
Mr "yEaH rIgHt" posted the following link about a week ago in answer to my question about removing event handlers. > http://www.vbinfozine.com/t_bindevt.shtml Following on from that post, the following issues still exist. The article shows how to find methods on a receiver that match the pattern OnXXXX given the sender. It loops through the sender events and tries to get methods from the receiver that match the pattern. For each one...
3
1958
by: Beth | last post by:
in the following: this.ExitButton.Click += new System.EventHandler(this.ExitButton_Click); if I saw an equation, such as y +=x; then y = y+x. But what is the meaning in the event handler. I realize it is adding an event handler for the click event of the exit button, but how do i reason through the 2 step process inferred by the += equation.
5
1888
by: Richard Grant | last post by:
Hi, I need to "save" in a variable the event handler sub of a control's event, then perform some process, and finally "restore" the originally saved event handler. Example in pseudo-code: 1) Save cmbMyCombo's event handler for SelectedIndexChange event. 2) Assign a temporary event handler sub to cmbMyCombo's for its SelectedIndexChange event.
1
2518
by: tdan | last post by:
I do not know how to get Event.stopObserving() to work in the context I am using it. I am displaying a Color Selection Table and attaching 2 events: 1. onmouseover to display the color to the user 2. onmouseup to select the color and hide the Table The onmouseup event should also call Event.stopObserving() so that mouse clicks are no longer being observed. An alert is shown when the mouse is clicked indicating the event handler is...
0
8197
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8640
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8589
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8287
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8443
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
4136
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2573
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1757
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1438
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.