473,605 Members | 2,703 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Dispose or not??

I used to think that all objects that implement
IDisposable should be disposed. But I found 80% of the
classes implement IDisposable.

But when I looked into the samples, most objects are not
disposed. Generally, they're Gdi objects, form controls,
most components, etc.

So why? How can I decide to dispose or not?
Nov 15 '05 #1
14 3119
Hi,

In most cases you don't have to call dispose method explicitly. The garbage
collector will dispose all unreachable object without puasing the interface.
C# GC manages them well, so most of these sample, you have refered have let
GC to mange it.

Nirosh.

"qrli" <an*******@disc ussions.microso ft.com> wrote in message
news:04******** *************** *****@phx.gbl.. .
I used to think that all objects that implement
IDisposable should be disposed. But I found 80% of the
classes implement IDisposable.

But when I looked into the samples, most objects are not
disposed. Generally, they're Gdi objects, form controls,
most components, etc.

So why? How can I decide to dispose or not?

Nov 15 '05 #2
Unless an object's class asks you to explicitly call Dispose, you don't have
to. The GC will take care.

Most GDI objects require you to call Dispose - to release their handle
internally.

vJ

"qrli" <an*******@disc ussions.microso ft.com> wrote in message
news:04******** *************** *****@phx.gbl.. .
I used to think that all objects that implement
IDisposable should be disposed. But I found 80% of the
classes implement IDisposable.

But when I looked into the samples, most objects are not
disposed. Generally, they're Gdi objects, form controls,
most components, etc.

So why? How can I decide to dispose or not?

Nov 15 '05 #3
qrli <an*******@disc ussions.microso ft.com> wrote:
I used to think that all objects that implement
IDisposable should be disposed. But I found 80% of the
classes implement IDisposable.
80%? Where do you get that figure? That sounds awfully high to me.
But when I looked into the samples, most objects are not
disposed. Generally, they're Gdi objects, form controls,
most components, etc.
Form controls will usually be disposed by the default code created by
VS.NET.
So why? How can I decide to dispose or not?


I would encourage you to always make sure you dispose of objects which
implement IDisposable.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #4
Champika Nirosh <cn*****@textce ntric.lk> wrote:
In most cases you don't have to call dispose method explicitly. The garbage
collector will dispose all unreachable object without puasing the interface.
C# GC manages them well, so most of these sample, you have refered have let
GC to mange it.


That's a bad idea though, as most types which implement IDisposable
either contain other IDisposables or directly contain native system
resources. Leaving those to the garbage collector (which only knows
about memory pressure, not Windows handle pressure etc) is a bad idea.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #5
Vijaye Raji <no************ @hotmail.com> wrote:
Unless an object's class asks you to explicitly call Dispose, you don't have
to. The GC will take care.


The GC will do it at *some* point, but the very fact that a type
implements IDisposable means it's telling you that it would be
preferable to call Dispose explicitly.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #6
Most classes in the following namespaces implement
IDisposable. And they're what I use most.
System.Componen tModel
System.Drawing
System.Windows. Forms

The code VS.NET generated doesn't dispose any thing
except Timer and ImageList. Only Timer and ImageList,
that is those have a constructor take an IContainer
parameter, are added to the components field and are
disposed in the Form.Dispose(bo ol).

-----Original Message-----
qrli <an*******@disc ussions.microso ft.com> wrote:
I used to think that all objects that implement
IDisposable should be disposed. But I found 80% of the
classes implement IDisposable.
80%? Where do you get that figure? That sounds awfully

high to me.
But when I looked into the samples, most objects are not disposed. Generally, they're Gdi objects, form controls, most components, etc.
Form controls will usually be disposed by the default

code created byVS.NET.
So why? How can I decide to dispose or not?
I would encourage you to always make sure you dispose of

objects whichimplement IDisposable.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
.

Nov 15 '05 #7
<an*******@disc ussions.microso ft.com> wrote:
Most classes in the following namespaces implement
IDisposable. And they're what I use most.
System.Componen tModel
System.Drawing
System.Windows. Forms
They may be what you use most when doing GUI stuff, but that's very
different from 80% of classes implementing IDisposable.
The code VS.NET generated doesn't dispose any thing
except Timer and ImageList. Only Timer and ImageList,
that is those have a constructor take an IContainer
parameter, are added to the components field and are
disposed in the Form.Dispose(bo ol).


It *does* dispose things - just not where you can see them. To
demonstrate this, create a simple form with just a label in it. Go into
the code and change it from creating a label to creating a MyLabel, and
define the MyLabel class as:

class MyLabel : Label
{
protected override void Dispose(bool disposing)
{
MessageBox.Show ("Disposing: "+disposing );
base.Dispose (disposing);
}
}

Run it, and close the form - you'll see a message box saying
"Disposing: true" which shows that Dispose has been called.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #8
That just proves that all objects are disposed when the
program shut down.

I can give you another example. In the D3D9 Wizard
generated code, D3DSettingForm was created and there's no
Dispose call.

It *does* dispose things - just not where you can see them. Todemonstrate this, create a simple form with just a label in it. Go intothe code and change it from creating a label to creating a MyLabel, anddefine the MyLabel class as:

class MyLabel : Label
{
protected override void Dispose(bool disposing)
{
MessageBox.Show ("Disposing: "+disposing );
base.Dispose (disposing);
}
}

Run it, and close the form - you'll see a message box saying"Disposing: true" which shows that Dispose has been called.
--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
.

Nov 15 '05 #9
qrli <an*******@disc ussions.microso ft.com> wrote:
That just proves that all objects are disposed when the
program shut down.
No - it's when the form itself is disposed (which it is automatically
on closing). To verify this, you could also do:

new Form1().Dispose (); // Or whatever your form is called

- and again, Dispose is called on all the controls added to it.
I can give you another example. In the D3D9 Wizard
generated code, D3DSettingForm was created and there's no
Dispose call.


Does D3DSettingsForm definitely not call Dispose internally when it's
closed though? I don't have the Managed DirectX SDK installed, but I
suspect it works the same way as the normal System.Windows. Forms does.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #10

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

Similar topics

3
6045
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 Dispose method of IDbConnection. This Dispose method is called from a destructor. In the dispose method, i also call GC.SuppressFinalize(this) so as to avoid finalizer. All this was OK untill i came across one article that says - not to call...
4
1919
by: RiteshDotNet | last post by:
..net Frame work 1. Dispose Method what it does ? A. who its call / when it calls ? B. Is it fire automatically ? c. When dispose method is call what it does ? D. Release a Object from memory or release refrence from memory ? E. If it release object from memory then what GC does ? 2. Which class can have dispose method ? class which is member of IDispose Inteface 3. Where we should use abstract / Interface ?
11
5283
by: Ken Durden | last post by:
I am in search of a comprehensive methodology of using these two object cleanup approaches to get rid of a number of bugs, unpleasantries, and cleanup-ordering issues we currently have in our 4-month old C#/MC++ .NET project project. I'd like to thank in advance anyone who takes the time to read and/or respond to this message. At a couple points, it may seem like a rant against C# / .NET, but we are pretty firmly stuck with this approach...
10
18469
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.
16
4245
by: Daniel Mori | last post by:
If an object implements the IDisposable interface (regardless if its a framework object or a user object), should I always dispose of that object out of principle?
3
2068
by: Maxim | last post by:
Hi! According to documenation, if we need to release some umanaged resources manually, we need to implement IDisposable interface with single Dispose method. I am just wondering, what will happen if I just create my own Dispose (or any other name) mehtod, without implementing the IDisposable interface. In all the examples, which I found, this Dispose method called from my user code (not by GC). What do I miss? Thanks.
1
1710
by: Billy | last post by:
Hello... I'm trying to make a database access class for an asp.net application. When I run my application, the Garbage Collecter doesn't seems to unload the memory attributed to my SQLConnection. My db access class has a method that returns a dataset and one that executes a non-reader request. It also has a dispose method (to release the connection) that I call in the finalize method of my aspx pages(as learn in "Implementing...
156
5817
by: Dennis | last post by:
Ok, I'm trying to dispose of every object that I create that has a dispose method based on advice from this newsgroup. However, I'm not sure how to dispose of the following object that was created inside a method call. dim myvar as new object1 object1.dosomethingmethod(new object2) Note that object 2 has a dispose method but how do I dispose of it unless I do the following:
71
10706
by: active | last post by:
In the main program I check to see if a certain form has been disposed. Does it make sense in that form's FormClosed event to do: Me.Dispose to make sure it is disposed the next time I check. Or is that automatic? Thanks
44
8214
by: Smokey Grindle | last post by:
I have a list box on my form, but I need to databind it to a data table that is a private member of the form's class... so I basically have Public Class MyForm priate m_MyTable as new datatable End Class now where would I properly dispose of this? In the finalize method? I am loading the data for the table in a subroutine that is executed at form load, of course all the commands tied to it are wrapped in using blocks, but
0
8424
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8415
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8069
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8286
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6742
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
3958
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2438
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
1537
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1270
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.