473,405 Members | 2,445 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,405 software developers and data experts.

events & destructor

When I attach an event handler (e.g. button.Click += new
EventHandler(Button_Clicked)) it's not necessary to remove the handler when
my class (or form) gets disposed or destroyed, is it? When my class gets out
of its scope and its time for GC-ing comes, it will be able to be destroyed
despite the "active" event handler?
Nov 16 '05 #1
3 2761
As soon as the Form becomes unreachable, that is, you have no
references to the Form object left, then the GC can clean up both the
form and the event handler (delegate) the Form references (assuming
the delegate in also unreachable). So no, you should not need to
remove the event handler.

HTH,

--
Scott
http://www.OdeToCode.com

On Sat, 4 Sep 2004 21:19:39 +0200, "lukasz" <bb****@op.pl> wrote:
When I attach an event handler (e.g. button.Click += new
EventHandler(Button_Clicked)) it's not necessary to remove the handler when
my class (or form) gets disposed or destroyed, is it? When my class gets out
of its scope and its time for GC-ing comes, it will be able to be destroyed
despite the "active" event handler?


Nov 16 '05 #2
How about the situation with two objects, ObjUser and ObjService, ObjService
valid throughout the life span of the application and ObjUser being created
for a short time at certain moments. Now when I write in ObjUser's creator:

ObjService.OnChanged += new EventHandler(this.xxxx)

do I need to explicitly remove this handler? ObjService will still be in use
when ObjUser gets out of its scope.
Uzytkownik "Scott Allen" <bitmask@[nospam].fred.net> napisal w wiadomosci
news:4s********************************@4ax.com...
As soon as the Form becomes unreachable, that is, you have no
references to the Form object left, then the GC can clean up both the
form and the event handler (delegate) the Form references (assuming
the delegate in also unreachable). So no, you should not need to
remove the event handler.

HTH,

--
Scott
http://www.OdeToCode.com

On Sat, 4 Sep 2004 21:19:39 +0200, "lukasz" <bb****@op.pl> wrote:
When I attach an event handler (e.g. button.Click += new
EventHandler(Button_Clicked)) it's not necessary to remove the handler whenmy class (or form) gets disposed or destroyed, is it? When my class gets outof its scope and its time for GC-ing comes, it will be able to be destroyeddespite the "active" event handler?

Nov 16 '05 #3
Hi lukasz:

In this case you might want to explicitly remove those handlers. If I
understand what you are saying - the Service will keep a reference to
the delegate and prevent it from being collected, and the delegate
holds a reference to to objUser which keeps objUser around.
This is an unfortunate scenario, you might want to google for "weak
reference delegate" as I believe some people have written code to
address the issue.

--
Scott
http://www.OdeToCode.com

On Sat, 4 Sep 2004 22:09:16 +0200, "lukasz" <bb****@op.pl> wrote:
How about the situation with two objects, ObjUser and ObjService, ObjService
valid throughout the life span of the application and ObjUser being created
for a short time at certain moments. Now when I write in ObjUser's creator:

ObjService.OnChanged += new EventHandler(this.xxxx)

do I need to explicitly remove this handler? ObjService will still be in use
when ObjUser gets out of its scope.
Uzytkownik "Scott Allen" <bitmask@[nospam].fred.net> napisal w wiadomosci
news:4s********************************@4ax.com.. .
As soon as the Form becomes unreachable, that is, you have no
references to the Form object left, then the GC can clean up both the
form and the event handler (delegate) the Form references (assuming
the delegate in also unreachable). So no, you should not need to
remove the event handler.

HTH,

--
Scott
http://www.OdeToCode.com

On Sat, 4 Sep 2004 21:19:39 +0200, "lukasz" <bb****@op.pl> wrote:
>When I attach an event handler (e.g. button.Click += new
>EventHandler(Button_Clicked)) it's not necessary to remove the handlerwhen >my class (or form) gets disposed or destroyed, is it? When my class getsout >of its scope and its time for GC-ing comes, it will be able to bedestroyed >despite the "active" event handler?
>


Nov 16 '05 #4

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

Similar topics

3
by: Trevor | last post by:
Hello, How can I setup an event in C#? I would like for class A to have an event, and class B to intercept the event. Class B should not be able to call the event itself, only class A. How is...
3
by: nobs | last post by:
Hi guys I think I missunderstud something with delegates & events Hopefully someone can bring some light in this issue: Following problem I have a class which provide some events (like...
3
by: Lance | last post by:
I've noticed that controls that are contained in MDI child forms fail to raise MouseLeave events if the MDI child form's MdiParent property is set to Nothing (after it was set to an existing MDI...
2
by: Mike | last post by:
Greetings, It would seem that this topic has been discussed at some length, but I was unable to discern whether there was a clear cut solution to resolve and/or otherwise workaround the issue. ...
0
by: BigAl.NZ | last post by:
Hi Guys, I am trying to write/copy some code that uses events with a GPS. Everytime the GPS position updates the event fires. The GPS code is from a SDK Library that I got called GPS Tools...
0
by: Terry Olsen | last post by:
In an effort to create a QueryBuilder for a web page, I've created a WebUserControl. It can be seen here: http://boycot.no-ip.com/images/querybuilder.jpg I load one control initially in the...
1
by: Scott M. | last post by:
I'm trying to get my head around what's involved in C# to declare, raise and respond to an event. I'm coming from the VB.NET world where to declare and event we wrote: Public Event Foo() To...
5
by: Spam Catcher | last post by:
Hi all When .NET fires and event, does the event handler execute under a new thread, or does it execute under the primary application thread? Basically if I have events firing, do the event...
5
by: =?Utf-8?B?QW50?= | last post by:
Hi, I am using a sqlCommand object to run a SP. The SP returns an output parameter that I retrieve the value of, & display. I want to use the StatementCompleted event of the command obj. to...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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...
0
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...

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.