Re: Is this pattern OK?
- From: "richard.markiewicz@xxxxxxxxxxxx" <richard.markiewicz@xxxxxxxxxxxx>
- Date: Fri, 18 Jan 2008 11:50:43 -0800 (PST)
On Jan 18, 2:32 pm, Jon Skeet [C# MVP] <sk...@xxxxxxxxx> wrote:
Ben Voigt [C++ MVP] <r...@xxxxxxxxxxxxx> wrote:
<snip>
What you ought to do instead is convert try / finally { Dispose(); } into
using blocks, which automatically handle the null case.
They also cope with the possibility of Dispose calls throwing
exceptions - with a single finally block, if an early Dispose call
fails, the rest won't get executed.
--
Jon Skeet - <sk...@xxxxxxxxx>http://www.pobox.com/~skeet Blog:http://www.msmvps.com/jon.skeet
World class .NET training in the UK:http://iterativetraining.co.uk
Thank you Jon.
So what you are saying is, if my original snippet looked like this:
finally
{
objSearchResults.Dispose(); //The troublesome line is now first in the
finally block
objSearchADAM.Dispose();
objADAM.Dispose();
}
That the second and third dispose statements would not be executed
after the first one threw an exception?
Do you know what Using{} does behind the scenes? Does it create a
try{} finally{} on the objects I "use"?
We had a lot of problems with System.DirectoryServices in .NET 1.1
where objects weren't disposed properly, and I'm keen not to go down
that route again. So it would be good to know that my revised pattern
is not subject to the same problem.
Many thanks, Richard
.
- Follow-Ups:
- Re: Is this pattern OK?
- From: Peter Duniho
- Re: Is this pattern OK?
- References:
- Is this pattern OK?
- From: richard.markiewicz@xxxxxxxxxxxx
- Re: Is this pattern OK?
- From: Ben Voigt [C++ MVP]
- Re: Is this pattern OK?
- From: Jon Skeet [C# MVP]
- Is this pattern OK?
- Prev by Date: Re: LINQ Dynamic Data Model
- Next by Date: Logged user with vista rights elevation
- Previous by thread: Re: Is this pattern OK?
- Next by thread: Re: Is this pattern OK?
- Index(es):
Relevant Pages
|