all groups > dotnet clr > july 2003 >
You're in the

dotnet clr

group:

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


Clear bindings on dispose of a control to allow GC to collect? Niall
7/4/2003 11:52:34 AM
dotnet clr: 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

RE: Clear bindings on dispose of a control to allow GC to collect? clyon NO[at]SPAM online.microsoft.com (
7/29/2003 9:16:40 PM
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


--------------------
[quoted text, click to view]
microsoft.public.dotnet.framework.clr,microsoft.public.dotnet.framework.wind
owsforms.databinding
[quoted text, click to view]
microsoft.public.dotnet.framework.windowsforms.databinding:1157
microsoft.public.dotnet.framework.clr:1332
[quoted text, click to view]


--

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.
Re: Clear bindings on dispose of a control to allow GC to collect? Niall
7/31/2003 9:49:58 AM
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

[quoted text, click to view]

AddThis Social Bookmark Button