Re: StatusStrip control leaks its collection items
- From: carl.clawson@xxxxxxxxxxxxx
- Date: Thu, 10 Jul 2008 21:37:16 -0700 (PDT)
On Jul 10, 6:00 pm, "Peter Duniho" <NpOeStPe...@xxxxxxxxxxxxxxxx>
wrote:
On Thu, 10 Jul 2008 13:55:31 -0700, <carl.claw...@xxxxxxxxxxxxx> wrote:
I don't expect to have to dispose things explicitly unless there's a
reason that I need to have it done at a specific point in the code.
That's what the garbage collector is for.
This is categorically WRONG.
Jack's reply addresses this, but because it's so important, let me be very
clear: if an object implements IDisposable, it's _essential_ that you call
Dispose() on the object when you're done with it. The garbage collector
is for releasing _managed_ memory. _Disposing_ objects is the exact
opposite of "what the garbage collector is for".
Pete
Sorry Pete, but I don't agree. Look at the standard Dispose/Finalize
pattern. like here: http://msdn.microsoft.com/en-us/library/s9bwddyx.aspx.
You'll see that when a disposable object is finalized, its unmanaged
resources are released. That's what the Dispose(False) in the
finalizer does. You need to dispose it only if you need to control
WHEN the unmanaged resources get released, or if you need to force
managed resources to be released before they would otherwise get
finalized. A good example is if you've opened a stream on a file and
need to re-open it. If you don't Dispose (or Close) the old stream,
you have to wait an unpredictable amount of time before the file will
open again because you've left a stream hanging around with an open
file handle that won't close until the garbage collector gets to it.
Now, it's certainly a good idea to dispose things when you're done
with them. No argument there. But I don't get why it's absolutely
necessary, and I've never had a problem until I bumped into the
StatusStrip. The finalizer will eventually take care of it as long as
all references to the object are gone. I would say that any finalizer
that fails to do so is buggy. There may be some special cases with
system-wide resources that don't get released on program exit because
I don't think finalizers always get called on exit. But that's not
what we're talking about here.
Theoretical arguments aside, I can do the very same experiment with
any other control, like buttons. Create a bunch, put them on a form,
take them off. Repeat indefinitely. You can watch the handle count
grow and then suddenly drop back repeatedly as the unused buttons get
garbage collected. So...why can I get away with not disposing buttons,
but I can't get away with not disposing ToolStripItems? I tried this
with a form full of buttons just now and the handle count never rises
much above 100 before dropping back down to a dozen or two.
-- Carl
.
- Follow-Ups:
- Re: StatusStrip control leaks its collection items
- From: Peter Duniho
- Re: StatusStrip control leaks its collection items
- References:
- StatusStrip control leaks its collection items
- From: carl . clawson
- RE: StatusStrip control leaks its collection items
- From: Family Tree Mike
- Re: StatusStrip control leaks its collection items
- From: carl . clawson
- Re: StatusStrip control leaks its collection items
- From: Peter Duniho
- StatusStrip control leaks its collection items
- Prev by Date: Re: StatusStrip control leaks its collection items
- Next by Date: Re: StatusStrip control leaks its collection items
- Previous by thread: Re: StatusStrip control leaks its collection items
- Next by thread: Re: StatusStrip control leaks its collection items
- Index(es):
Loading