Scott,
the GC can pick up the objects, should I do this in the finalize method?
No!
Three reasons:
First: Think about it, if the Finalize method is being called, then the GC
must have "picked up" the object.
Second: If the finalize method is being called on the object handling the
event, then the object offering the event may also be finalizable & already
finalized. In other words the object you are attempting to remove or remove
from may not exist!
Third: Finalize is to be used for unmanaged resources (Win32 file/GDI
handles), adding a Finalize method to classes can hurt performance, as those
objects will survive at least 2 collections of the garbage collector (adding
memory pressure).
Normally the object that called AddHandler is the object that I use to call
RemoveHandler as that is the object the "owns" the handler itself. For
example in a collection of objects that want to handle the events of
contained objects, I call AddHander in the collections Add method &
RemoveHandler in the Remove method. I may then have this collection
implement the IDisposable pattern & the Dispose method to remove all the
objects & their handlers, especially if the collection can go away without
having the objects go away.
Hope this helps
Jay
Scott Meddows said:
I'm adding event handlers for dynamically loaded assemblies into a
program. I know that I must unregister those event handlers so