473,386 Members | 1,810 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,386 software developers and data experts.

Diff Between Setting an object to NULL and Dispose()

wats the Diff Between Setting an object to NULL and calling the
Dispose() method for that object?

Jan 18 '06 #1
4 1894
1.
DBConnection conn = new DBConnection(); // consturct one
DBConnection object
conn=null; // there is no reference to the consturcted object, and the
resource managed by the DBConnection object will be released when GC
starts, maybe a few minutes later, maybe a long time later.

2.
DBConnection conn = new DBConnection(); // consturct one
DBConnection object
conn.dispose; // the conn still refer to the consturcted object, but
the resource managed by the DBConnection object is released right now.

Jan 18 '06 #2
DBConnection con = new DBConnection();
DBConnection a = new DBConnection();
//At this stage both objects do hav a refrence,and their refrences are
held by the objects con and a

DBConnection obj;
DBConnection b;
//The objects obj and b hav nothing referred to, so wat about the
resources of these objects???

//So if they dont hav a reference is it equivalent to
con =null;
//So ive set con to null , so no reference here and their resources?
wat about them??
//At this stage is obj, b and con have no reference so wat about their
resources?

a.dispose();
//If i use dispose with object a then the reference is still there, but
the resource is released? is that right?

Jan 19 '06 #3
> DBConnection con = new DBConnection();
DBConnection a = new DBConnection();
//At this stage both objects do hav a refrence,and their refrences are
held by the objects con and a
Here you have created two new DBConnection objects by virtue of using
the new keyword.

DBConnection obj;
DBConnection b;
//The objects obj and b hav nothing referred to, so wat about the
resources of these objects???
Here you have not created any objects but two variables that can later
refer to objects, but the objects themselves, have not been created
yet. Because they have not been created yet, there are no resources
used.

//So if they dont hav a reference is it equivalent to
con =null;
No it's not, because when you declared con, you alse created a new
object using the new keyword.
//So ive set con to null , so no reference here and their resources?
wat about them??
When you set con to null all you did was point con to nothing. The
original object still exists and its resources are still in use. Only
when the GC gets around to collecting it, will they be gone. There is
no way to know when this will happen.
//At this stage is obj, b and con have no reference so wat about their
resources?
Again, because you never created an object for obj and b, there were no
resources used. For con, the resources will be cleaned up by the GC
when it runs, but you dont know when that will be.

a.dispose();
//If i use dispose with object a then the reference is still there, but
the resource is released? is that right?


By calling a.dispose, you are explicitly releasing the resources used
by a, but there is still a reference. Although the GC may still
collect it.

Jan 19 '06 #4
That was very kind of you, Dunaway. Your answer really cleared the
doubts i had for long time....thank you soo much

Jan 20 '06 #5

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

Similar topics

0
by: faktujaa | last post by:
Hi, I receive the above error in my code when in debug mode but im not using serviced components instead im making use of sql server transaction. In short, I have defined a transaction class that...
10
by: Henrik Dahl | last post by:
Hello! After I've finished using an instance of the SqlCommand class, should I then invoke Dispose() on the instance. I suppose so, as there is a Dispose method, but what does it actually...
0
by: Stuart Norris | last post by:
Dear Group, I am having a problem setting SocketOptionName.SendTimeout on a client TCPIP application using the sockets in .NET. From the on-line help it is possible to set a...
1
by: Rachel Suddeth | last post by:
I have an form where the whole display is a tab control (well, that plus a status bar.) I want to set the focus to the first TextBox on the first TabPage when it loads. I tried to put that into...
14
by: MuZZy | last post by:
Hi, Lately i've been (and still am) fixing some memory leaks problems in the project i just took over when i got this new job. Among the other issues i've noticed that for localy created objects...
3
by: Adriano | last post by:
Hello, when I try to print something, either DataGrid or from Crystal Report viever the folowing error message appears and cancels printing: Object reference not set to an instance of an...
1
by: hansolox1 | last post by:
The following program simply sets the icon of a form called form1. When I get the name of the embedded icon using GetManifestResourceNames(), I store the name in a string variable called s. The...
2
by: Steve | last post by:
This is a weird one. I have a series of "SmartParts" which are CAB (Composite Application Block) Views which are finally just UserControls (99% of the time) Anyway, I layout my UserControl in...
8
by: Andrus | last post by:
..NET 2 Winforms application. How to create new setting and set it default value in userSettings section of app.config file or overwrite existing setting value ? I found code below in this list...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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,...

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.