Re: Disposing framework objects
Dave wrote:[color=blue]
> "Andreas Huber" <ah2003@gmx.net > wrote in message
> news:3f21b610$1 @news.swissonli ne.ch...[color=green]
>> Dave wrote:
>> [snip][color=darkred]
>>> Adam Nathan's blog posted an example where not calling Dispose on an
>>> object
>>> which wrapped a native handle had the potential for the object being
>>> garbage
>>> collected too soon.[/color]
>>
>> Adam Nathan himself says that this is a bug in the implementation of
>> the RegistryKey class. Calling Dispose() would sure prevent the bug
>> from manifesting, but to me this is more of a side-effect than the
>> root of the problem. In other words: What the heck are finalizers
>> for, if correctness mandates that we call Dispose() on *all* objects
>> with unmanaged resources anyway?[/color]
>
> It may be a bug/limitation in the Registry class, but at this point
> in time if we don't take that into account then it will show up as a
> flaw in our code when we utilize it. There is no guarantee that there
> aren't many other classes with the same problem, both those written
> by MSFT developers and especially those written by non-MSFT
> developers, who are less likely to be aware of the potential flaws.
>
> Also, the problem Nathan described is not that someone did not call
> Dispose, rather, it was caused by the code not maintaining a
> reference to the object alive long enough to ensure that the object
> would not be collected until the code had actually finished using it.
> The call to Dispose served to maintain the reference to the object
> until after the call that used the object had finished executing.[/color]
Yes, that was clear.
[color=blue][color=green]
>> The problem somewhat resembles the one of temporary object lifetime
>> in C++. I vaguely recall that in the pre-standard days (~1995) there
>> were huge discussions of lifetime issues of (stack-allocated)
>> temporary object created during expression evaluation. E.g.
>>
>> Matrix r, a, b, c, d;
>>
>> // fill matrices
>>
>> r = a * b * c * d;
>>
>> Here, the C++ standard mandates that the temporary created by a * b
>> must live until the result has been assigned to r, although the
>> temporary is no longer needed when it has been multiplied with c,
>> the results of which will be stored in a new temporary object. It
>> seems that the problem Nathan documents only crops up because there
>> aren't similar rules in C#.
>>[/color]
>
> This is more of a lifetime issue then a Dispose/finalizer issue,
> though there is some relationship between the two - calling Dispose
> when done with an object ensures a reference to it exists until it is
> no longer needed. I doubt there will be rules written into the C#
> language that will prevent this sort of problem.[/color]
I agree, given how GC works.
[color=blue]
> Nathan didn't say it
> was a language problem so much as it was an implementation problem.[/color]
I know. I found the behavior disturbingly counter-intuitive, that's why I
suggested there might be a way to fix this within the language.
Regards,
Andreas
Dave wrote:[color=blue]
> "Andreas Huber" <ah2003@gmx.net > wrote in message
> news:3f21b610$1 @news.swissonli ne.ch...[color=green]
>> Dave wrote:
>> [snip][color=darkred]
>>> Adam Nathan's blog posted an example where not calling Dispose on an
>>> object
>>> which wrapped a native handle had the potential for the object being
>>> garbage
>>> collected too soon.[/color]
>>
>> Adam Nathan himself says that this is a bug in the implementation of
>> the RegistryKey class. Calling Dispose() would sure prevent the bug
>> from manifesting, but to me this is more of a side-effect than the
>> root of the problem. In other words: What the heck are finalizers
>> for, if correctness mandates that we call Dispose() on *all* objects
>> with unmanaged resources anyway?[/color]
>
> It may be a bug/limitation in the Registry class, but at this point
> in time if we don't take that into account then it will show up as a
> flaw in our code when we utilize it. There is no guarantee that there
> aren't many other classes with the same problem, both those written
> by MSFT developers and especially those written by non-MSFT
> developers, who are less likely to be aware of the potential flaws.
>
> Also, the problem Nathan described is not that someone did not call
> Dispose, rather, it was caused by the code not maintaining a
> reference to the object alive long enough to ensure that the object
> would not be collected until the code had actually finished using it.
> The call to Dispose served to maintain the reference to the object
> until after the call that used the object had finished executing.[/color]
Yes, that was clear.
[color=blue][color=green]
>> The problem somewhat resembles the one of temporary object lifetime
>> in C++. I vaguely recall that in the pre-standard days (~1995) there
>> were huge discussions of lifetime issues of (stack-allocated)
>> temporary object created during expression evaluation. E.g.
>>
>> Matrix r, a, b, c, d;
>>
>> // fill matrices
>>
>> r = a * b * c * d;
>>
>> Here, the C++ standard mandates that the temporary created by a * b
>> must live until the result has been assigned to r, although the
>> temporary is no longer needed when it has been multiplied with c,
>> the results of which will be stored in a new temporary object. It
>> seems that the problem Nathan documents only crops up because there
>> aren't similar rules in C#.
>>[/color]
>
> This is more of a lifetime issue then a Dispose/finalizer issue,
> though there is some relationship between the two - calling Dispose
> when done with an object ensures a reference to it exists until it is
> no longer needed. I doubt there will be rules written into the C#
> language that will prevent this sort of problem.[/color]
I agree, given how GC works.
[color=blue]
> Nathan didn't say it
> was a language problem so much as it was an implementation problem.[/color]
I know. I found the behavior disturbingly counter-intuitive, that's why I
suggested there might be a way to fix this within the language.
Regards,
Andreas
Comment