By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,795 Members | 1,235 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,795 IT Pros & Developers. It's quick & easy.

Can AddHandler cause memory leaks?

P: n/a
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

Jan 4 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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
"TrtnJohn" <Tr******@discussions.microsoft.com> wrote in message
news:47**********************************@microsof t.com...
| 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
|
Jan 4 '06 #2

P: n/a
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.

"Jay B. Harlow [MVP - Outlook]" wrote:
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
"TrtnJohn" <Tr******@discussions.microsoft.com> wrote in message
news:47**********************************@microsof t.com...
| 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
|

Jan 4 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.