467,116 Members | 1,255 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,116 developers. It's quick & easy.

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
  • viewed: 1642
Share:
2 Replies
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Pavils Jurjans | last post: by
1 post views Thread by Aamir Mahmood | last post: by
14 posts views Thread by Hamed | last post: by
6 posts views Thread by Jake K | last post: by
2 posts views Thread by Gabe Moothart | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.