473,394 Members | 1,797 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Simple question about dispose() and = null

Hi,

I have come across codes like this

if(dbConn != null)
{
dbConn.dispose();
}

and sometimes like

dbConn.dispose();

if(dbConn != null)
{
dbConn = null;
}

I was wondering what is the correct way of cleaning up?

Thanks

Joey
Nov 16 '05 #1
4 1977
Joey wrote:
I was wondering what is the correct way of cleaning up?

Class instances often encapsulate control over resources that are not
managed by the runtime, such as database connections and similar. You
can provide implicit control over how these resources are managed by
implementing a destructor in the class. The garbage collector calls this
method at some point after there are no longer any valid references to
the object.

If an external resource is scarce or expensive, you might want to
provide the ability to explicitly release these resources before the
garbage collector frees the object. To provide explicit control,
implement the IDisposable interface.

If you implement IDisposable, you should provide implicit cleanup using
a destructor.

Here is an example of a best-practice implementation of IDisposable:
public class MyClass : IDisposable {
public void Dispose() {
Dispose(true);
GC.SuppressFinalize(this);
}

protected virtual void Dispose(bool disposing) {
if (disposing) {
// Free other managed objects.
}
// Free unmanaged objects and set large fields to null.
}

~MyClass() {
Dispose (false);
}
}

For more info on when you use destructors and when to implement the
IDisposable interface see
http://msdn.microsoft.com/library/de...izeDispose.asp

Anders Norås
http://dotnetjunkies.com/weblog/anoras/
Nov 16 '05 #2
Hi, Joey!

class MyDisposableClass : IDisposable
{

private SqlConnection _connection;
....

public void Dispose()
{
this.Dispose( true );
GC.SuppressFinalize( this );
}

private void DisposeConnection()
{
if ( _connection == null ) return;
_connection.Dispose();
_connection = null;
}

protected virtual void Dispose( bool disposing )
{
if ( disposing ){
DisposeConnection();
}
}

~MyDisposableClass()
{
this.Dispose( false );
}

}

--
WBR, Roman S. Golubin
ICQ UIN: 63253392

Nov 16 '05 #3
Anders Norås <an**********@objectware.no> wrote:

<snip>
If you implement IDisposable, you should provide implicit cleanup using
a destructor.


That's not always true. If you implement IDisposable in order to call
Dispose on other objects, there's usually no need for a destructor.

If you contain unmanaged resources directly, *that's* the place for a
destructor.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4
dbConn.dispose();

if(dbConn != null)
{
dbConn = null;
}


This code is malformed. There is no point checking to see whether dbConn
is null after calling Dispose. If dbConn is null, then the Dispose call
is going to raise a null pointer exception. Dispose itself is not going
to set the dbConn variable to null.

The other code example made sense. Or you could do this:

if(dbConn != null)
{
dbConn.Dispose();
dbConn = null;
}

The if statement isn't necessary if you can find a way to employ a using
statement.

H^2
Nov 16 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: faktujaa | last post by:
Hi All, A small confusion. I have defined a connection class that has System.Data.IDbConnection as a member variable and implements IDisposable interface. I have implemented Dispose method to call...
4
by: xyu | last post by:
Hello, First I would like to thank anyone who helps me, much appreciated. I'm a c++ programmer just started using c#. I'm writing some big hash table so I want to actively take my object off...
0
by: Tal Sharfi | last post by:
Hi everyone I recently had the need for StringGrid object same as the one that Delphi has. An object that helps show lists of other objects in a simple grid. I searched the news groups and...
2
by: afatdog | last post by:
Form1: //----------------------------------------------------------------- public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.TextBox textBox1; private...
0
by: afatdog | last post by:
Form1: //----------------------------------------------------------------- public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.TextBox textBox1; private...
0
by: Robert Ludig | last post by:
How do I bind a textbox to a simple string varaible with databinding? I managed to do the binding but unfortnatedly the textvox does not get updated when I change the string wich the textbox is...
12
by: Jeff B. | last post by:
If an object implements the IDisposable interface, should I always call the Dispose method or is just setting it to null and letting the GC handle it sufficient? Here is the pattern I've been...
1
by: Chris Dunaway | last post by:
I have created a simple Extender Provider and when I drop it onto a form, it appears in the component tray, but none of the controls it is supposed to provide a property for show the property in...
10
by: Nemisis | last post by:
HI everyone, I think i am on the right path but if someone could confirm it, that would be great. In my business Layer i have my business object, for this example, called User. User has...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.