473,545 Members | 1,956 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1907
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
1259
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 is derived from connection class. In the constructor, i open a connection if one not already present and then start the transaction by using...
10
18412
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 release? I would basically prefer to skip invoking Dispose() as this will free me from determining when the usage actually has finished.
0
2344
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 SocketOptionName.SendTimeout for sends on TCPIP sockets. In all the tests that I have done with both Async and Sync sends the send returns immediately with the...
1
6326
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 the FormLoad event, but the form still comes up with the focus on the tab for the first tab page. Anyone know how to do this? Here is what I...
14
3129
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 it makes difference to explicitly put them to null after working with them is done - it somehow makes the GC collect them sooner, like here: void...
3
2425
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 object :(((( Anyone pls help me to solve this problem!!! thanks in advance,
1
2280
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 value of s is "test2.icon1.ico". This program will compile and run just fine from visual studio .net 2003 using .net 1.1 and also will compile and...
2
5044
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 the VS2005 designer, but at runtime, the controls are in different locations and different sizes. I have been able to restore their sizes by putting...
8
16086
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 which modifies Application setting section but how to add new item to userSettings ? Andrus.
0
7415
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7675
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7775
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5997
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5344
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4963
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3470
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1902
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1030
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.