473,796 Members | 2,509 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Dispose

aaj
Hi

can anyone point to a good link explaining (simply) when I should use the
dispose method.

Sort of things I'd like to know are

how does it differ from finalize in VB.net (or is it nothing like finalize)
Does the garbage collector automatically call the dispose method when an
object goes out of scope.
Does every object support it, if not, which do
Does it have to be called manually for every object that does support it
....

this sort of thing

thanks

Andy
Nov 17 '05 #1
4 2377
1. Finalize is different from Dispose. Finalize is like a destructor, while
Dispose is like a Free() call (sort of)
2. The garbage collector will not call the Dispose method
3. Not every object supports Dispose. Any class that implements IDisposable
will support it.
4. Yes, you need to call it manually (or use the "using" keyword, in which
case the compiler will generate code to call Dispose)

Dispose is where you would free up any resource that you are hanging on to.
That would be a good place to close files or close any Database connections
you have open. Not every object needs it.

-vj

"aaj" <aa*@aaj.com> wrote in message
news:1115968104 .9b46c80adea689 3baec8ff88f2232 3aa@teranews...
Hi

can anyone point to a good link explaining (simply) when I should use the
dispose method.

Sort of things I'd like to know are

how does it differ from finalize in VB.net (or is it nothing like
finalize)
Does the garbage collector automatically call the dispose method when an
object goes out of scope.
Does every object support it, if not, which do
Does it have to be called manually for every object that does support it
...

this sort of thing

thanks

Andy

Nov 17 '05 #2

"aaj" <aa*@aaj.com> wrote in message
news:1115968104 .9b46c80adea689 3baec8ff88f2232 3aa@teranews...
Hi
Hi
can anyone point to a good link explaining (simply) when I should use the
dispose method.
Ther are plenty Just google.
Sort of things I'd like to know are

how does it differ from finalize in VB.net (or is it nothing like
finalize) - It is nothing like finalize.
Does the garbage collector automatically call the dispose method when an
object goes out of scope. - Nope. You have to do that yourself. Have a look at the using keyword.
Does every object support it, if not, which do - Typically classes that uses external resources like SqlConnection and
StreamReader. FxCop is a nifty tool that will tell you if you missed calling
Dispose on such objects.
Does it have to be called manually for every object that does support it - Yep. (again see the using keyword)
thanks

Andy


Andy, how the GC works, and what makes finalizers in .NET so very different
from a destructor in unmanaged runtimes like C++ and Delphi; is of grave
importance. You really should take a break from coding and start reading all
about it. This will teach you why we need the Dispose pattern in .NET.

Happy Coding, err Reading...
- Michael S
Nov 17 '05 #3
My philosophy is that if an object is disposable it's probably for a reason.
If Dispose exists, use it when your done with the object.

Only objects that implement IDisposable will have Dispose methods.

Finalize may call Dispose but that isn't certain and will depend upon the
person that wrote the object.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.

"aaj" <aa*@aaj.com> wrote in message
news:1115968104 .9b46c80adea689 3baec8ff88f2232 3aa@teranews...
Hi

can anyone point to a good link explaining (simply) when I should use the
dispose method.

Sort of things I'd like to know are

how does it differ from finalize in VB.net (or is it nothing like
finalize)
Does the garbage collector automatically call the dispose method when an
object goes out of scope.
Does every object support it, if not, which do
Does it have to be called manually for every object that does support it
...

this sort of thing

thanks

Andy

Nov 17 '05 #4
Aaj,

See this message from Jay B. which describes it the best I have seen until
now.

http://groups-beta.google.com/group/...b1e6ae59?hl=en

I hope this helps,

Cor
Nov 17 '05 #5

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

Similar topics

3
6077
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
1935
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
5313
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
18549
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
4267
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
2079
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
1735
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
5908
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
10763
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
8276
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
9680
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10456
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...
1
10174
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,...
1
7548
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6788
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5442
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5575
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4118
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
2
3731
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.