Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / CLR / July 2003

Tip: Looking for answers? Try searching our database.

Clear bindings on dispose of a control to allow GC to collect?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Niall - 04 Jul 2003 02:52 GMT
I've been using the .Net Memory Profiler to chase down a memory leak in our
application. A custom control we have written was not being collected after
the form it was on was closed. Because the form tied to an event on that
control, the whole lot wasn't being collected.

Using the profiler, I found out there was only one root path to this custom
control. It was being referenced by a ComponentEntry which was being held in
a static Hashtable - TypeDescriptor.cachedComponentEntries. Examining the
allocation stack of that ComponentEntry showed that it was created when the
custom control was bound and DataBindings.Add() was called. This lead me to
think that perhaps if I ensured the control was unbound when it was
disposed, the reference would be broken and the control could be collected.
So I put a DataBindings.Clear() in the dispose, and lo and behold, the
control got collected.

So I am wondering... is it required/expected behaviour that a control unbind
itself when it is being disposed? Would there be any possible negative
effects from explicitly removing the bindings in the Dispose? Is this a .Net
memory leak such that the framework code should recognise when the control
is disposing and clear its cache, or perhaps use a WeakReference?

Niall
"Chris Lyon [MSFT]" - 29 Jul 2003 22:16 GMT
Niall,

If you want the control to be collected, you'll need to sever all
references to it.  In this case, unbinding it.  Although not required, the
Dispose method is the recommended place for this.

This isn't a memory leak in the Framework.  The developer must put in the
unbinding code, so that when Dispose gets called, the object is cleaned up.

-Chris

--------------------
>From: "Niall" <asdf@me.com>
>Subject: Clear bindings on dispose of a control to allow GC to collect?
[quoted text clipped - 6 lines]
>Message-ID: <#Ber46cQDHA.1072@TK2MSFTNGP10.phx.gbl>
>Newsgroups:
microsoft.public.dotnet.framework.clr,microsoft.public.dotnet.framework.wind
owsforms.databinding
>NNTP-Posting-Host: 64.215.221.9
>Path: cpmsftngxa09.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP10.phx.gbl
>Xref: cpmsftngxa09.phx.gbl
microsoft.public.dotnet.framework.windowsforms.databinding:1157
microsoft.public.dotnet.framework.clr:1332
>X-Tomcat-NG: microsoft.public.dotnet.framework.clr
>
[quoted text clipped - 20 lines]
>
>Niall

Signature

This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm

Note:  For the benefit of the community-at-large, all responses to this
message are best directed to the newsgroup/thread from which they
originated.

Niall - 31 Jul 2003 00:49 GMT
Chris,

Thank you for your reply. The reason why I asked is that while memory
profiling, I have discovered a few times references to our objects being
held by static hashtables in the Framework. Sometimes it's not clear to know
exactly what responsibilities you have when cleaning up your object in these
cases, because you're not aware there is even a reference being held from a
static object until you run a memory profile and see your object being
referenced from within the framework. I've had a similar case where
MenuItems were being cached in a static hashtable in the Framework...

Are these kind of things documented? I haven't seen anything before your
post which said I had to make sure I cleared the bindings as the control was
being disposed.

Thanks,

Niall

> Niall,
>
[quoted text clipped - 18 lines]
> >Message-ID: <#Ber46cQDHA.1072@TK2MSFTNGP10.phx.gbl>
> >Newsgroups:

microsoft.public.dotnet.framework.clr,microsoft.public.dotnet.framework.wind
> owsforms.databinding
> >NNTP-Posting-Host: 64.215.221.9
[quoted text clipped - 29 lines]
> >
> >Niall

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.