Re: StatusStrip control leaks its collection items

Tech-Archive recommends: Fix windows errors by optimizing your registry



On Thu, 10 Jul 2008 21:37:16 -0700, <carl.clawson@xxxxxxxxxxxxx> wrote:

[...] _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 true. But there's no guarantee that an object will _ever_ be finalized, never mind when.

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,

But that's all the time. There's not ever a situation in which correct code can leave disposal of unmanaged resources up to the indeterminate rules offered by the garbage collector.

[...]
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,

It's absolutely necessary because without managing the disposal of unmanaged resources yourself you have no guarantee they will ever be managed, nor do you have any deterministic behavior regarding the disposal and other cleanup of unmanaged resources.

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.

Again, there is no guarantee that this will happen.

I would say that any finalizer
that fails to do so is buggy.

You would be wrong then. The finalizer does exactly what it's documented to do, but finalizing objects isn't part of its guarantee.

[...]
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.

I can't answer the specific question because I don't have a concrete code sample with which to compare, nor do I really care. The fact is, since ..NET never guarantees that an object will be finalized, and since you are required by the .NET contract to dispose objects that implement IDisposable, the fact that you leak resources when you fail to dispose such objects is a bug in your code, not in .NET.

Disagree all you want. The fact remains that by your own admission, if you'd followed the .NET requirement of disposing disposable objects, you wouldn't have a problem. That may not be incontrovertible proof, but it's sure a heck of a strong suggestion.

Pete
.


Quantcast