Re: Dispose Again!
On 2005-03-22, Michael C# <xyz@abcdef.com > wrote:[color=blue]
>
> "David" <dfoster@woofix .local.dom> wrote in message
> news:slrnd3u6f2 .vtv.dfoster@wo ofix.local.dom. ..[color=green]
>> On 2005-03-21, Michael C# <xyz@yomomma.co m> wrote:[color=darkred]
>>>
>>> After all, I tested the third option with the
>>> SqlCommand object in this thread days ago, and still haven't received a
>>> simple "YES" or "NO" concerning whether or not it should be Disposed of![/color]
>>
>> OK, a simple answer. Disposing a SqlCommand does absolutely nothing
>> except take up time. It's a no-op.
>>[/color]
>
> Thank you! Finally a straight answer! My next question is this: is the
> optimizer smart enough to figure this out and discard the no-op?[/color]
I don't see how, that's why it takes up (a little) time. There's a lock
and a few tests involved.
[color=blue][color=green]
>> I see your general point here, but in practice it's not that tough. In
>> day-to-day programming, I pretty much deal with only four types of
>> objects:
>>
>> Web controls, which never need to be disposed.
>>
>> Forms controls, which should always be disposed.
>>
>> Data library objects, and since there's only a few of them I tend to
>> know the ones I'm working with pretty damn well since I'm hitting the db
>> all day long, and so I have a pretty good sense as to whether I want to
>> dispose them and when.
>>
>> Things that derive from Component, which generally don't need to be
>> disposed, but it only takes a moment to figure out if the class actually
>> implements a Dispose function, so in practice that's not a problem.[/color]
>
> Now see, this is where the confusion comes in. You say Forms controls
> should always be disposed - yet others say Dispose is not necessary on a
> Label... Is the Label an "exception" ? Or should Forms controls, including
> the Label, be disposed?[/color]
In practice, it's really not an issue for me. I add it to components
and I know the components will be disposed for me. I realize there are
designs where this is a real issue, but so far it's just not an issue to
me.
[color=blue]
> Also, at the end you mention "it only takes a moment to figure out if the
> class actually implements a Dispose function..." which takes us full circle
> to the crux of the matter - some say that not everything that implements a
> Dispose needs to be Disposed. The question is which Components that expose
> a Dispose method need to have Dispose called on them?[/color]
I know what Component.Dispo se does, and MarshallByRefCo mponent.Dispose , etc. So
the only issue is whether the class itself implements a needed Dispose
override, and that only takes a second to check.
[color=blue][color=green][color=darkred]
>>> Alas, it's much simpler to consistently Dispose() of my
>>> SqlCommand, SqlConnection and other objects when I'm done with them. I
>>> would love to see a demonstration of how all the proposed research into
>>> this
>>> arcane trivia generates anything greater than a personal sense of
>>> accomplishment. ..[/color]
>>
>> I actually think avoiding the Dispose calls makes code a lot cleaner.[/color]
>
> That may be true, but I think it may be short-term thinking. Per another
> message in here, if the SqlCommand, for instance, were to be implemented
> differently in the future - and the Developer suddenly recommends the use of
> Dispose - you will have to re-tool your design patterns and re-work your
> code.[/color]
And I do worry about that, but there's a flipside. I see a lot of code
that does a premature Dispose that only works because Dispose doesn't
really do anything. If some class I'm calling suddenly implement a
truly destructive dispose, my code might have a few temporary leaks but
that's better than simply crashing. And I do somewhat trust that decent
programmers will implement Finalize correctly.
I'm not saying this is right, just that it's a trade-off. Dispose is
basically a hole in the Framework due to (necessary) limitations of the
GC. I don't think there's a simple one-size-fits-all solution to it.
On 2005-03-22, Michael C# <xyz@abcdef.com > wrote:[color=blue]
>
> "David" <dfoster@woofix .local.dom> wrote in message
> news:slrnd3u6f2 .vtv.dfoster@wo ofix.local.dom. ..[color=green]
>> On 2005-03-21, Michael C# <xyz@yomomma.co m> wrote:[color=darkred]
>>>
>>> After all, I tested the third option with the
>>> SqlCommand object in this thread days ago, and still haven't received a
>>> simple "YES" or "NO" concerning whether or not it should be Disposed of![/color]
>>
>> OK, a simple answer. Disposing a SqlCommand does absolutely nothing
>> except take up time. It's a no-op.
>>[/color]
>
> Thank you! Finally a straight answer! My next question is this: is the
> optimizer smart enough to figure this out and discard the no-op?[/color]
I don't see how, that's why it takes up (a little) time. There's a lock
and a few tests involved.
[color=blue][color=green]
>> I see your general point here, but in practice it's not that tough. In
>> day-to-day programming, I pretty much deal with only four types of
>> objects:
>>
>> Web controls, which never need to be disposed.
>>
>> Forms controls, which should always be disposed.
>>
>> Data library objects, and since there's only a few of them I tend to
>> know the ones I'm working with pretty damn well since I'm hitting the db
>> all day long, and so I have a pretty good sense as to whether I want to
>> dispose them and when.
>>
>> Things that derive from Component, which generally don't need to be
>> disposed, but it only takes a moment to figure out if the class actually
>> implements a Dispose function, so in practice that's not a problem.[/color]
>
> Now see, this is where the confusion comes in. You say Forms controls
> should always be disposed - yet others say Dispose is not necessary on a
> Label... Is the Label an "exception" ? Or should Forms controls, including
> the Label, be disposed?[/color]
In practice, it's really not an issue for me. I add it to components
and I know the components will be disposed for me. I realize there are
designs where this is a real issue, but so far it's just not an issue to
me.
[color=blue]
> Also, at the end you mention "it only takes a moment to figure out if the
> class actually implements a Dispose function..." which takes us full circle
> to the crux of the matter - some say that not everything that implements a
> Dispose needs to be Disposed. The question is which Components that expose
> a Dispose method need to have Dispose called on them?[/color]
I know what Component.Dispo se does, and MarshallByRefCo mponent.Dispose , etc. So
the only issue is whether the class itself implements a needed Dispose
override, and that only takes a second to check.
[color=blue][color=green][color=darkred]
>>> Alas, it's much simpler to consistently Dispose() of my
>>> SqlCommand, SqlConnection and other objects when I'm done with them. I
>>> would love to see a demonstration of how all the proposed research into
>>> this
>>> arcane trivia generates anything greater than a personal sense of
>>> accomplishment. ..[/color]
>>
>> I actually think avoiding the Dispose calls makes code a lot cleaner.[/color]
>
> That may be true, but I think it may be short-term thinking. Per another
> message in here, if the SqlCommand, for instance, were to be implemented
> differently in the future - and the Developer suddenly recommends the use of
> Dispose - you will have to re-tool your design patterns and re-work your
> code.[/color]
And I do worry about that, but there's a flipside. I see a lot of code
that does a premature Dispose that only works because Dispose doesn't
really do anything. If some class I'm calling suddenly implement a
truly destructive dispose, my code might have a few temporary leaks but
that's better than simply crashing. And I do somewhat trust that decent
programmers will implement Finalize correctly.
I'm not saying this is right, just that it's a trade-off. Dispose is
basically a hole in the Framework due to (necessary) limitations of the
GC. I don't think there's a simple one-size-fits-all solution to it.
Comment