When/How to release an event handler allocated in a contructor

  • Thread starter Thread starter Kenneth Baltrinic
  • Start date Start date
K

Kenneth Baltrinic

I have a custom collection class that raises OnAdd and OnRemove events. It
is used by another class (Class A) as a container for the class's children.
Thus in class A's constructor, I instantiate an instance of the collection
class and register event handlers for the two events. For a give instance
of Class A, when is the appropriate time to release these handlers prior to
the instances destruction?

--Ken Baltrinic
 
Kenneth,

You don't have to worry about this. When you create the delegates to
attach to your event handler, you are storing a reference to an instance of
a class (the delegate) which holds an instance to another class (the
callee). When you set the last reference to your object to null, the
references it holds do not count towards keeping another instance alive.
Because of that, you don't have to do anything.

Hope this helps.
 
Back
Top