Thanks Richard!
Following on from this there is another thing i am not 100% clear on. Which
of these is correct for proper disposal of objects:
EXAMPLE 1:
Product p = null;
string productNameList = String.Empty;
for(int i = 0; i < productIdList.Length; i++)
{
p = ProductCatalog.GetProduct(productIdList[i].ToString());
productNameList += p.Name + ",";
}
p.Dispose();
p = null;
EXAMPLE 2:
Product p = null;
string productNameList = String.Empty;
for(int i = 0; i < productIdList.Length; i++)
{
p = ProductCatalog.GetProduct(productIdList[i].ToString());
productNameList += p.Name + ",";
p.Dispose();
p = null;
}
Would example 1 leave resources undisposed of? I'm assuming the answer is
yes and that example 2 would be the way to go but it would be good to get
confirmation.
Cheers,
Nick
----- Original Message -----
From: "Richard Blewett" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Wednesday, October 06, 2004 2:28 PM
Subject: Re: [ADVANCED-DOTNET] Avoiding Dispose?
The problem you have outlined is the one big issue with IDisposable IMO. The
problem being that Idisposable has no way of describing semantics and
ownership. The crucial question in your code is "who has ownership of the
Product?". If the product as been created on the fly by the catalog and
passed out then the calling code should call Dispose. If the Catalog is
simply handing out a reference to an object it is storing in a collection
then the last thing you want is the calling code to call Dispose as it will
leave a zombie object in the collection for when the next caller requests
the same object.
In this situation I think it is best to not hand out the Product itself but
rather render a data object with the pieces of the Product's state that are
of interest from the Product and pass that out. In that case the data object
doesn't implement IDisposable and so there is no ambiguity about who should
call Dispose.
By the way, I'm not arguing that IDisposable is a bad thing it just doesn't
solve *every* problem with resource management.
Regards
Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog
-----Original Message-----
From: Unmoderated discussion of advanced .NET topics.
[mailto:[EMAIL PROTECTED] On Behalf Of Nick Simpson
Sent: 06 October 2004 14:16
To: [EMAIL PROTECTED]
Subject: Re: [ADVANCED-DOTNET] Avoiding Dispose?
[EMAIL PROTECTED] wrote:
> > In my code i have a method with just one line - as follows:
> >
> > return
> > ProductCatalog.GetProduct(productId).GetCategoryProperties().Tables
> > [0].DataRow[0]["Title"];
> >
> > The result of the GetProduct(productId) bit is an object that has a
> > Dispose
> > () method. Do i need to worry about calling this Dispose method?
> > That would mean breaking down the above into multiple lines as follows:
> >
> > string title = String.Empty;
> > Product p = ProductCatalog.GetProduct(productId);
> > title = p.GetCategoryProperties().Tables[0].DataRow[0]["Title"];
> > p.Dispose();
> > p = null;
> > return title;
> >
> > Is one of these better than the other from a resource management
> > point of view? Any help appreciated.
> >
>
> If a class implements IDisposable then to be on the safe side, you'll
> want to call Dispose as soon as you are done with an instance of the
> class. You can avoid breaking the code into 4-5 lines with code like
> this:
>
> using (Product p = ProductCatalog.GetProduct(productId))
> return p.GetCategoryProperties().Tables[0].DataRow[0]["Title"];
>
> Regards,
> Sami
My understanding of the "using" statement (if used properly) is that it will
result in pretty much the same outcome as the second option i suggested
above but just be syntactically different. What i want to know is if the
first line of code above actually needs disposing. Maybe another question to
ask to get to the answer i want is: if a reference to an object never exists
does it need disposing of? My gut feeling on the above is it should be
broken down but it would be good to get an explaination.
Nick
===================================
This list is hosted by DevelopMentor® http://www.develop.com
Some .NET courses you may be interested in:
Essential .NET: building applications and components with CSharp
August 30 - September 3, in Los Angeles
http://www.develop.com/courses/edotnet
View archives and manage your subscription(s) at http://discuss.develop.com