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
-----------------------------------