Not sure about this one

  • Thread starter Thread starter Michael Rodriguez
  • Start date Start date
M

Michael Rodriguez

Is this the correct way to write this?

DataSet ds;
try
{
ds = SomeFunctionThatReturnsADataSet();

// do some stuff with ds
...
}
finally
{
ds.Dispose();
}

Do I need to dispose it since I didn't explicitly create it? Can it hurt?

TIA,

Mike Rodriguez
 
Disposal of objects is normally up to the object that created it. If the method you are calling does not maintain a reference to
the DataSet, then you should dispose of it.
 
This could should compile every time should run sometimes:)

I think u assumed that SomeFunctionThatReturnsADataSet will return a valid
dataset object everytime. It will never return null. If it is possible,
u should modify your code

DataSet ds;
try
{
ds = SomeFunctionThatReturnsADataSet();

// do some stuff with ds
...
}
finally
{
if (ds!=null)
ds.Dispose();
}

And for your question, DataSet class derives from MarshalByValueComponent
which derives from IDisposable. At this point my advice u to modify your
code as given below;

using (DataSet ds =SomeFunctionThatReturnsADataSet() )
{
}

Every time, whenever program counter leaves using's scope compiler will
automatically call dispose method for u. It is not important if an exception
is thrown or how program counter leaves your function.

--
HTH

Thanks,
Yunus Emre ALPÖZEN
BSc, MCSD.NET
 
Michael,

I would do it like this:

using (DataSet ds = SomeFunctionThatReturnsADataSet())
{
// Do some stuff with ds
}

This way, if an exception is thrown, then you don't have to worry about
it being disposed (and if the function throws an exception, the dataset will
not be returned).

Hope this helps.
 
In answer to the question you posed at the end, the issue is not which
method explicitly created the object, but which method (if any) has
exclusive control of it when it "dies".

For example, if you put the DataSet in an ArrayList, the ArrayList
would then conceptually "own" that DataSet object, and the ArrayList
should Dispose of the DataSet when the ArrayList itself is no longer
needed (so you would need to subclass ArrayList in this case).

"Who owns what" is very important in object-oriented programming. It's
important to make the distinction between "I'm handing you a reference
to this object for your use, but the object is still mine," and "Here's
an object for you... I wash my hands of it... it's yours now."

Usually functions that return object references are doing the latter,
not the former.
 
Bruce Wood said:
In answer to the question you posed at the end, the issue is not which
method explicitly created the object, but which method (if any) has
exclusive control of it when it "dies".

"Who owns what" is very important in object-oriented programming. It's
important to make the distinction between "I'm handing you a reference
to this object for your use, but the object is still mine," and "Here's
an object for you... I wash my hands of it... it's yours now."

I think I understand now. So if my function looks like this:

public DataSet SomeFunctionThatReturnsADataSet()
{
DataSet ds1 = new DataSet();
// some code to fill the dataset here...
return ds1;
}

Then when I write:

DataSet ds = SomeFunctionThatReturnsADataSet();

ds now contains the reference to the dataset that got initialized in the
function. The function didn't dispose of it, so now it's up to the caller.

Thanks for the help!

Mike Rodriguez
 
Michael said:
I think I understand now. So if my function looks like this:

public DataSet SomeFunctionThatReturnsADataSet()
{
DataSet ds1 = new DataSet();
// some code to fill the dataset here...
But as others have said, if there is an exception thrown here it will
bubble up to the catch/finally block and will try and dispose a null object.

seriously consider the using construct.

using(dataset ds = SomeFunc....())
{
}
return ds1;
}

Then when I write:

DataSet ds = SomeFunctionThatReturnsADataSet();

ds now contains the reference to the dataset that got initialized in the
function.
Unless there was an exception before the initialised ds got returned.
The function didn't dispose of it, so now it's up to the caller.
This is correct.
 
John B said:
But as others have said, if there is an exception thrown here it will
bubble up to the catch/finally block and will try and dispose a null
object.

seriously consider the using construct.

using(dataset ds = SomeFunc....())
{
}

Hi John,

You are of course correct. I think I'm going to go with the

if (ds != null)
ds.Dispose();

Thanks to all for the help,

Mike Rodriguez
 
Back
Top