Re: StatusStrip control leaks its collection items
- From: "Peter Duniho" <NpOeStPeAdM@xxxxxxxxxxxxxxxx>
- Date: Fri, 11 Jul 2008 09:38:41 -0700
On Fri, 11 Jul 2008 07:49:26 -0700, <carl.clawson@xxxxxxxxxxxxx> wrote:
I agree that garbage collection is non-deterministic. No argument
there. I also agree that calling Dispose when possible is a very good
practice. It's what I normally do.
It's not just "very good practice". It's mandatory for correct code.
As an example, here's a quote from MSDN: "To provide explicit control, implement the Dispose provided by the IDisposable. The consumer of the object should call this method when it is finished using the object." And from the same page "Note that even when you provide explicit control using Dispose, you should provide implicit cleanup using the Finalize method. Finalize provides a backup to prevent resources from permanently leaking if the programmer fails to call Dispose."
http://msdn.microsoft.com/en-us/library/b1yfkh5e.aspx
They aren't saying that implementing IDisposable provides an optional approach to managing unmanaged resources. They are saying that if an object goes to the trouble of implementing IDisposable, then the user of that object is expected to call Dispose() when they're done with it, and that the finalizer exists in order to prevent incorrect code from creating permanent problems.
But I still maintain that an object that wraps an unmanaged resource
must release that resource in its finalizer. If the finalizer never
gets called, so be it. The object can't help that.
You are confusing two different issues. An object that maintains unmanaged resources must implement a finalizer, and must release those unmanaged resources in the finalizer. So you got that part correct. However, the finalizer doesn't exist for correct code. It exists for incorrect code. That is, it's a backup plan for dealing with code that is buggy.
In a bug-free program, a finalizer of an object implementing IDisposable is never called.
Now back to the problem at hand, I am finding that controls removed
from the StatusStrip appear to either A) never get finalized, which
means a reference to them is probably being held somewhere because
other deleted controls ARE getting finalized,
Without a complete program there is no way to know for sure why your controls aren't getting finalized. But that's not relevant. The fact is, your own code has absolutely no business _relying_ on finalization. You _must_ dispose the controls yourself if you expect your code to work correctly and reliably.
Now, you may in fact find that even if you call Dispose(), you still have a bug somewhere. For example, if the controls are actually still being referenced somewhere else, and something tries to access them after they're disposed, an exception will probably be thrown.
But at least you'll know that the code that thinks it's cleaning up the controls is disposing them properly.
In fact, if anything this illustrates why relying on the non-deterministic behavior of finalization is such a bad idea. It pushes other bugs farther out, making them harder to find and fix.
Pete
.
- Follow-Ups:
- Re: StatusStrip control leaks its collection items
- From: carl . clawson
- 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
- Re: StatusStrip control leaks its collection items
- From: carl . clawson
- Re: StatusStrip control leaks its collection items
- From: Peter Duniho
- Re: StatusStrip control leaks its collection items
- From: carl . clawson
- StatusStrip control leaks its collection items
- Prev by Date: Re: StatusStrip control leaks its collection items
- Next by Date: Re: Namespace question
- Previous by thread: Re: StatusStrip control leaks its collection items
- Next by thread: Re: StatusStrip control leaks its collection items
- Index(es):
Relevant Pages
|