using IDisposable

G

Guest

I have Class A that implements IDisposable, within this class i have method A that creates a new Sqlconnection object and execute some stored proc, in Class B, I create an instance of Class A object with the using statement..
my question is when my using statement ends Class A's dipose method should be called i think.. but will my Sqlconnection object within Class A Method A be disposed automatically as well?
 
L

Lynn Harrison

The Dispose method is not called automatically. You should call it at the
point that you wish to dispose the class's resources.

--

Lynn Harrison
SHAMELESS PLUG - Introduction to 3D Game Engine Design (C# & DX9)
www.forums.Apress.com

newbie said:
I have Class A that implements IDisposable, within this class i have
method A that creates a new Sqlconnection object and execute some stored
proc, in Class B, I create an instance of Class A object with the using
statement...
my question is when my using statement ends Class A's dipose method should
be called i think.. but will my Sqlconnection object within Class A Method A
be disposed automatically as well?
 
M

Marty McDonald

If you want the SqlConnection object to be destroyed when class A is
destroyed, two things need to happen.

First you need to code it yourself in the Dispose method. Something like...
public void Dispose()
{
cnn = null;
}

Second, class B must invoke the Dispose method. Something like...
a.Dispose;

Another option - You can also just forget about IDispsable and the Dispose
method, and instead, just destroy class A. That will automatically make the
connection object go out of scope and the CLR will get rid of it. But it is
good style to use Dispose so that consumers know that they can release
expensive resources when they are done with your object.
 
J

Jon Skeet [C# MVP]

Marty McDonald said:
If you want the SqlConnection object to be destroyed when class A is
destroyed, two things need to happen.

First you need to code it yourself in the Dispose method. Something like...
public void Dispose()
{
cnn = null;
}

That doesn't destroy the connection - given that something is disposed,
it's usually going out of scope anyway. You should instead call Dispose
on the connection, and follow the MS guidelines for writing Dispose
methods and finalizers:

http://tinyurl.com/2k6e
 
J

J.Marsch

Actually, newbie is using a using statement. If you use "using" with a
class that implements IDisposable, Dispose() will be called automatically
when the using block is exited.
 
J

J.Marsch

A little more to it than this: You can't deterministically destroy an
object in managed code, that's handled by the garbage collector, so this:
public void Dispose()
{
cnn = null;
}

Does not ensure that cnn is destroyed immediately, it merely becomes
available for collection. Further, it's not really necessary to manually
set the reference to null unless the object that owns cnn is going to be
referenced longer than cnn.

Once all references go away, the garbage collector will destroy the
connection when it gets around to it -- and that can be quite a while,
depending upon what generation cnn is in; I've seen gen2 objects stick
around for an hour after all references go away. You use Dispose to ensure
that any expensive resources that the object is holding on to are released
immediately (usually these are unmanaged resources), rather than waiting for
the object to be collected.
 
J

J.Marsch

If you want to have Class A dispose of the connection when it is disposed:
(and you want to make the dispose kind of deterministic for your using
clause):

public class A : IDisposable
{
private Connection cnn = new Connection();
public void Dispose()
{
// bear in mind that once you do this, cnn is useless, so it's
probably
// a bad thing if your consumer tries to use class A after Dispose
has been called
cnn.Dispose();
}
}

Now if Dispose is called, you can clean up early, otherwise the GC will take
care of things in the normal course.

One thing NOT to do: Don't use a Finalizer (Destructor in C# syntax)!

These are bad. Really bad. You only need to use one of these things if you
are directly holding an unmanaged resource (like a window handle, or a win32
file handle or something like that).


newbie said:
I have Class A that implements IDisposable, within this class i have
method A that creates a new Sqlconnection object and execute some stored
proc, in Class B, I create an instance of Class A object with the using
statement...
my question is when my using statement ends Class A's dipose method should
be called i think.. but will my Sqlconnection object within Class A Method A
be disposed automatically as well?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top