473,761 Members | 4,739 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Can AddHandler cause memory leaks?

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 DocumentComplet e 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
2 6500
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******@discu ssions.microsof t.com> wrote in message
news:47******** *************** ***********@mic rosoft.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 DocumentComplet e 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
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******@discu ssions.microsof t.com> wrote in message
news:47******** *************** ***********@mic rosoft.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 DocumentComplet e 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1869
by: Steve Binney | last post by:
My code makes synchronous HttpWebRequest and HttpRebResponse calls. In VS 2003, I am getting memory leaks and event handle leaks. I am closing all streams and using "using"statements. I have used .Net memory profiler from Sci Tech to analyze the leaks and they are coming from inside HttpWebRequest and HttpRebResponse. When I run my code on VS2005, I do not get any leaks. Is this problem fixed in .Net 1.1 SP1? I may not be able to wait...
4
2351
by: Morten Aune Lyrstad | last post by:
Ok, now I'm officially confused. I have a large project going, which uses a win32 ui library I am developing myself. And I'm getting weird memory leaks. I don't know if I can explain what is going on, but I really need some help on this one. Ok, so I have this class defined (written by Randy Charles Morin, www.kbcafe.com) which detects memory leaks. It creates a memory check point in the constructor, and another in the destructor, and...
2
4958
by: Generic Usenet Account | last post by:
I have been using STL for a long time now, without any problems. Recently we generated a purification report on our software using Rational Purify, and we found some memory leaks. My colleague claims that some of the memory leaks are due to the fact that "STL is wrought with memory leaks". Of course I disagree. I think that there are no "inherent leaks" with STL, but if used improperly, leaks will occur. One common source of leak that...
4
2303
by: NewToCPP | last post by:
Is there any debugging mechanism to find out which part of the code is causing memory leak?
4
5727
by: ali.jan | last post by:
Hi, It is trivial to load an assembly in a new Application Domain. Is there any way of loading an assembly in a new process? I tried using the Process class like this: Process p = new Process() p.StartInfo.FileName = mStartupFile p.StartInfo.UseShellExecute = False
23
4566
by: James | last post by:
The following code will create memory leaks!!! using System; using System.Diagnostics; using System.Data; using System.Data.SqlClient; namespace MemoryLeak
12
7142
by: Tom | last post by:
I use dynamically created controls all the time. I.E. I create the control in code then use AddHandler to add the necessary delegates for processing (like Click, etc). Does one have to call RemoveHandler after processing is done? In other words, when the form is closing do I have to do a RemoveHandler for each one of the controls I created? Or can I just let .NET handle it when the form is closed and garbage collection is done? Tom
6
5575
by: nmehring | last post by:
I have an MFC app with 2000 users. I have one user that experiences a crash in our software anywhere from 1 to 5 times a week when opening a particular module. No other users have reported this particular crash so I don't think anyone else is experiencing it but I know other users are doing exactly what she is doing because it is our most popular module. I have analyzed the dmp files from several of this user's crashes using windbg...
16
5104
by: graham.keellings | last post by:
hi, I'm looking for an open source memory pool. It's for use on an embedded system, if that makes any difference. Something with garbage collection/defragmentation would be nice. It should have the ability to allocate different size chunks of memory not just a single size. It should error check for double free, etc. And it should be usable by a mixture of C and C++ subsystems. If I get that, I'm happy. Thank you very much.
0
9554
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10136
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9989
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9925
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8814
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5266
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5405
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3509
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2788
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.