473,503 Members | 1,669 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

freeing resources

Hello,
I am new to c# and have become confused about freeing resources. I need to
know if using an objects Dispose method does the same thing as setting the
object to null?

Pen bluePen = new Pen( Color.Blue, 2 );
//use pen
bluePen.Dispose();

as opposed to:

Pen bluePen = new Pen(Color.Blue, 2);
//use pen
bluePen = null;

I appreciate your help
thank you
bill
Jul 19 '06 #1
3 1391
No, it's different.

1Call dispose will explicitly release the resources and the resource
will be freed immediately.

2Set it to null will rely on the garbage collection to free it up.

The 1is the recommended way to free the resource.

billq wrote:
Hello,
I am new to c# and have become confused about freeing resources. I need to
know if using an objects Dispose method does the same thing as setting the
object to null?

Pen bluePen = new Pen( Color.Blue, 2 );
//use pen
bluePen.Dispose();

as opposed to:

Pen bluePen = new Pen(Color.Blue, 2);
//use pen
bluePen = null;

I appreciate your help
thank you
bill

Jul 19 '06 #2
Also, see Using statement (the one about freeing
resources).
Roger
I am new to c# and have become confused about freeing resources. I need
to
know if using an objects Dispose method does the same thing as setting the
object to null?

Pen bluePen = new Pen( Color.Blue, 2 );
//use pen
bluePen.Dispose();

as opposed to:

Pen bluePen = new Pen(Color.Blue, 2);
//use pen
bluePen = null;

I appreciate your help
thank you
bill


Jul 19 '06 #3
This (the using statment) is an important point. If you use a class that
needs disposing, and it is not disposed, it can cause some serious problems,
depending on the class being used. A file that is opened, for example, will
cause the file to remain locked, until the app exits. So, it is important to
ensure that Disposable objects are Disposed. This *can* be done using
try/catch/finally, as in:

try
{
Pen bluePen = new Pen( Color.Blue, 2 );
//use pen
}
finally
{
if (bluePen != null) bluePen.Dispose();
}

However, if you use a using statement, it is much simpler:

using (Pen bluePen = new Pen(Color.Blue, 2)
{
//use pen
}

The using statement automatically disposes the object created when it passes
out of scope.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Chicken Salad Alchemist

What You Seek Is What You Get.

"Roger" <ro***@pcsrevenuecontrol.comwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
Also, see Using statement (the one about freeing
resources).
Roger
>I am new to c# and have become confused about freeing resources. I need
to
>know if using an objects Dispose method does the same thing as setting
the
object to null?

Pen bluePen = new Pen( Color.Blue, 2 );
//use pen
bluePen.Dispose();

as opposed to:

Pen bluePen = new Pen(Color.Blue, 2);
//use pen
bluePen = null;

I appreciate your help
thank you
bill



Jul 19 '06 #4

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

Similar topics

3
1315
by: | last post by:
Hi, What is the best method to close database connections? Is it enough if we just use the Close() method. Does the connection get released immediately? or do we have to do anything in the...
10
2060
by: dmanhr | last post by:
Hi! I'd like to free-up - destroy (myself not GC) object n (Node) with implementing Dispose(). I have linked list: class Node{ CNode nextNode; object data;
6
2746
by: Fernando Cacciola | last post by:
Help me out here please: While watching Brad Abraham's MSDN TV talk about the Dispose pattern, refering to: public virtual void Dispose ( bool disposing ) { if ( disposing ) { <-- WHAT...
15
1750
by: yehoshua | last post by:
Is there anyway to explicitly tell the garbage collector you are done with an object? In my app I have a point where I am sure there should be no references to an set of objects (well i remove...
7
2405
by: Adam | last post by:
I have a managed cpp wrapper. Im using this in a native dll as a static variable. I need to free this library when the dll is done being used. The perfect place to do this is DllMain for...
6
2708
by: copx | last post by:
Can you / are you supposed to free() string literals which are no longer needed? In my case I've menu construction code that looks like this: menu_items = list_new(); list_add(menu_items,...
11
1322
by: mark | last post by:
Please consider the following thread setup intended to free-up the UI while a huge process takes place: (declare delegates) (A B and C all global) Private Sub AB_Click(ByVal sender As...
5
3140
by: Amogh | last post by:
Hi, My question is related to setting freed pointers to NULL. After freeing a pointer: 1) Should the freeing routine also be responsible for setting the pointer to null? 2) Or, should the...
11
1976
by: vivek | last post by:
Hello, I have a pointer to a main structure which again consists of structures, enums, char, int, float and again complex structures. When i free all the contents of the main structure, it...
0
7273
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
7451
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...
0
5572
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,...
1
5000
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...
0
4667
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...
0
3150
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1501
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 ...
1
731
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
374
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.