Can AddHandler cause memory leaks?

G

Guest

If you call AddHandler and hook events of one object to a method in a class
will the event source object still be available for garbage collection if the
original reference is set to Nothing? Or do you have to call RemoveHandler
as well?

For example: I am using the ActiveX version of the WebBrowser control. In
the DocumentComplete event I set up my event handlers for the Document object
loaded by the browser. Do I have to call RemoveHandler every time I load a
new document so the original document will be free to be garbage collected?

I guess I am confused on what referencing occurs when you use AddHandler. I
couldn't find a good explanation on it in the MSDN Help. (I am sure it is
there somwhere). Any links are appreciated.

Thanks,
John
 
J

Jay B. Harlow [MVP - Outlook]

John,
When you use AddHandler (or WithEvents) the object handling the event will
hold a reference to the object raising the event. As the object handling the
event has the list of delegates. If both objects go "out of scope" at the
same time (such as when you close a Form) then the GC will collect all
objects.

However if object raising the event goes "out of scope" way before the
object handling the event, then the object raising event will survive until
the object handling the event is "out of scope".

By "out of scope" I mean that all references to the said objects are
released, except for implicit & explicit references to each other.

| Do I have to call RemoveHandler every time I load a
| new document so the original document will be free to be garbage
collected?

You shouldn't need to as the Document is holding references to your object
with the handlers.

--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


| If you call AddHandler and hook events of one object to a method in a
class
| will the event source object still be available for garbage collection if
the
| original reference is set to Nothing? Or do you have to call
RemoveHandler
| as well?
|
| For example: I am using the ActiveX version of the WebBrowser control.
In
| the DocumentComplete event I set up my event handlers for the Document
object
| loaded by the browser. Do I have to call RemoveHandler every time I load
a
| new document so the original document will be free to be garbage
collected?
|
| I guess I am confused on what referencing occurs when you use AddHandler.
I
| couldn't find a good explanation on it in the MSDN Help. (I am sure it is
| there somwhere). Any links are appreciated.
|
| Thanks,
| John
|
 
G

Guest

So, I assume, the only way to manually release these references is to call
RemoveHandler.

Your answer about the Document object confuses me. If AddHandler created a
reference to the Document object then why would the previous Document go "out
of scope"? The object receiving the event will not go away when it loads a
new Document.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top