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

Question about event handlers and object scope.

If an object of class A adds an event handler to and object of class B, and
class A is finalized, does the handler delegate get removed from class B's
event handler or is there a bad pointer on class B's event handler? In
other words, do I have to put a destructor in class A to remove its event
method from class B's event handler?

Example:

class B
{
public B() { }

public delegate void HandlerDelegate(object sender, EventArgs e);
public event HandlerDelegate SomeEvent;
}

class A
{
public A(B b)
{
b.SomeEvent += new B.HandlerDelegate(MyEventHandler);
}

protected void MyEventHandler(object sender, EventArgs e)
{

}
}

--
-----------------------------------
Ken Varn
Senior Software Engineer
Diebold Inc.

EmailID = varnk
Domain = Diebold.com
-----------------------------------
Nov 15 '05 #1
2 1822
Ken,

If class B is holding a delegate which points to a method in object A,
then A can not be finalized, because A is still accessible through B. As
long as B is accessible and holds the delegate to the method on the instance
of A, A can not be eligible for GC.

Because of this, if you want A to be eligible for GC, you have to
explicitly remove the event handler from B.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Ken Varn" <nospam> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
If an object of class A adds an event handler to and object of class B, and class A is finalized, does the handler delegate get removed from class B's
event handler or is there a bad pointer on class B's event handler? In
other words, do I have to put a destructor in class A to remove its event
method from class B's event handler?

Example:

class B
{
public B() { }

public delegate void HandlerDelegate(object sender, EventArgs e);
public event HandlerDelegate SomeEvent;
}

class A
{
public A(B b)
{
b.SomeEvent += new B.HandlerDelegate(MyEventHandler);
}

protected void MyEventHandler(object sender, EventArgs e)
{

}
}

--
-----------------------------------
Ken Varn
Senior Software Engineer
Diebold Inc.

EmailID = varnk
Domain = Diebold.com
-----------------------------------

Nov 15 '05 #2
Could this be done by adding a destructor for class A and have the
destructor remove the delegate from class B? In know that in this
particular scenario, class B may be finalized before class A, but lets
suppose class B's event member is static.

--
-----------------------------------
Ken Varn
Senior Software Engineer
Diebold Inc.

EmailID = varnk
Domain = Diebold.com
-----------------------------------
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:O7*************@TK2MSFTNGP11.phx.gbl...
Ken,

If class B is holding a delegate which points to a method in object A,
then A can not be finalized, because A is still accessible through B. As
long as B is accessible and holds the delegate to the method on the instance of A, A can not be eligible for GC.

Because of this, if you want A to be eligible for GC, you have to
explicitly remove the event handler from B.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Ken Varn" <nospam> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
If an object of class A adds an event handler to and object of class B,

and
class A is finalized, does the handler delegate get removed from class B's event handler or is there a bad pointer on class B's event handler? In
other words, do I have to put a destructor in class A to remove its event method from class B's event handler?

Example:

class B
{
public B() { }

public delegate void HandlerDelegate(object sender, EventArgs e);
public event HandlerDelegate SomeEvent;
}

class A
{
public A(B b)
{
b.SomeEvent += new B.HandlerDelegate(MyEventHandler);
}

protected void MyEventHandler(object sender, EventArgs e)
{

}
}

--
-----------------------------------
Ken Varn
Senior Software Engineer
Diebold Inc.

EmailID = varnk
Domain = Diebold.com
-----------------------------------


Nov 15 '05 #3

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

Similar topics

10
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...
2
by: Pavils Jurjans | last post by:
Hello, I wanted to propose a small class that would help to overcome the feature that's missing in MSIE. I'd like to get some feedback from people and, perhaps, improvements in code/other ideas:...
8
by: Dennis C. Drumm | last post by:
I have a class derived from a SortedList called SystemList that contains a list of objects indexed with a string value. The definition of the objects contained in the SortedList have a boolean...
1
by: Aamir Mahmood | last post by:
Hi all, Consider the following code as just an example to understand what I want to say. My question is if a class instance subscribes one or more events of another class. If that instance...
16
by: Hamed | last post by:
Hello I am developing a utility to be reused in other programs. It I have an object of type Control (a TextBox, ComboBox, etc.) that other programmers use it in applications. they may set some...
14
by: Hamed | last post by:
Hello It seems that I should implement ICloneable to implement my own clone object. the critical point for me is to make a control object based on another control object that all of its event...
6
by: Jake K | last post by:
Where is the best place to define event handlers? I need to define event handlers for a COM object referenced in my project and am doing so in the fowm load. Is this the best place?
2
by: Gabe Moothart | last post by:
Hello, In one of my asp.net applications, I create a series of checkboxes, set their properties, and give them an "onChecked" event handler on the fly using an anonymous method. The code looks...
3
by: Tuxedo | last post by:
Is it possible to add a function call to the onUnload event handler from an external js file, or can this only be done via the body tag? In any case, I presume there can only exist one onUnload...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.