does event increase obj's ref?

  • Thread starter Thread starter void
  • Start date Start date
V

void

suppose
A a = new A ();
B b = new B ();
b.OneEvent += new EventHandler(a.handler());
//dose this event increase a's ref in managed code.
a = null;
GC.Collect();

b.OneEvent(null, null);
//OK?
 
First, in managed code there is no refcount. When the
garbage collector starts collecting it conciders each
object ready for collection until the opposite has been
proven.
To answer your question, yes, the event is the reason that
object a is not collected. An event is implemented as a
MultiCastDelegate, which keeps a linked list with
references to registered eventhandlers.

If you want to remove the event, you can do two things:
1. Deregister your eventhandler:
b.OneEvent -= new EventHandler(a.handler());
2. Or implement the add and remove 'methods'of your event
and use weak references.

By the way, if you're finished using object b, the GC will
collect the delegate that are in the invocationlist of
your event.


Gert-Jan.
 
Back
Top