Dispose Again!

G

Guest

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:

dim myvar as new object1
dim mydisposableobject as new object 2
object1.dosomethingmethod(mydisposableobject )
myotherobject.Dispose
 
G

Guest

I thought so. I see lots of examples in MSDN that use the first code so I
guess they don't dispose of the objects and leave it to the GC.
 
B

Bob Powell [MVP]

lots of examples in MDSN.

Yes.. sloppy lazy and often wrong..

I guess if you do a lot of that sort of thing you can try to convert the
object to IDisposable and dispose of it if it succeeds.

--
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.
 
B

Bob Powell [MVP]

To be fair.. Not disposing of some objects is perfectly ok because the CG
will get 'em in the end. Often though, and this is particularly true for
GDI+ graphics, a good preemptive Dispose is good practice as can be forcing
GC.

A lot of MSDN examples don't include the dispose because they don't need to
confuse the issue with such trivialities in a 5 line example. For the
purpose of that example this is fine.

The technique will come back to bite you later however and it's necessary to
be aware of the implications of disposing or not.

--
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.
 
J

Jay B. Harlow [MVP - Outlook]

Bob,
a good preemptive Dispose is good practice as can be forcing GC.
From what I've read forcing a GC most of the time will do more harm then
good...

http://blogs.msdn.com/ricom/archive/2004/11/29/271829.aspx

http://blogs.msdn.com/ricom/archive/2003/12/02/40780.aspx

I do however agree that a good preemptive Dispose is good practice...

Hope this helps
Jay

Jay


Bob Powell said:
To be fair.. Not disposing of some objects is perfectly ok because the CG
will get 'em in the end. Often though, and this is particularly true for
GDI+ graphics, a good preemptive Dispose is good practice as can be
forcing GC.

A lot of MSDN examples don't include the dispose because they don't need
to confuse the issue with such trivialities in a 5 line example. For the
purpose of that example this is fine.

The technique will come back to bite you later however and it's necessary
to be aware of the implications of disposing or not.
<<snip>>
 
S

Scott M.

Whether or not you dispose an object doesn't have any affect on what the GC
will or won't do. Dispose is just a method that the class designer creates
and places code to clean up any unmanaged resources that the class may use.
GC is a separate issue.
 
S

Scott M.

Yes, forcing a GC.Collect is general NOT a good idea for several reasons.

First, you may not need memory freed up at the time you call GC.Collect and
so the CPU cycles you use cleaning up memory actually may cause you
application to run slower than normal.

Second, when you call GC.Collect, the GC runs in the same thread as your
application and potentially causes you application to run slower than
normal.

The whole point of Garbage Collection is to allow the GC to collect as it
deems necessary. As soon as you intervene in that, you will most likely
make the process run less efficiently.

-Scott
 
H

Herfried K. Wagner [MVP]

Dennis,

Dennis said:
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:

dim myvar as new object1
dim mydisposableobject as new object 2
object1.dosomethingmethod(mydisposableobject )
myotherobject.Dispose

If the class has a 'Dispose' method, call this method when you don't need
the object any more in order to release unmanaged resources (file handles,
window handles, etc.) occupied by the instance. The second snippet shows
the preferred way, which is calling 'Dispose'. Note that the GC will
automatically dispose the object when finalizing it. However, by calling
'Dispose' you can reduce the number of unmanaged ressources, which is very
important if there is only a limited number of such resources.
 
J

JD

That depends if the class design follows the IDisposable pattern where the
Finalize method is overriden also. Notice in the IDisposable pattern if a
call to Dispose is made, a call to GC.SuppressFinalize is also called. This
will prevent object from being placed on the Finalization queue and will
prevent the object from going through an additional garbage collection. Look
at "Implementing a Dispose Method" in the MSDN documentation.
 
C

Cor Ligthert

Dennis,

When you inherit from Components than there is already Idispose implemented
in the base class. (By instance all classes in Sytem.Data and
System.Windows.Forms.Control inherit from Components) You just have to do
nothing because it is done by the base class when that class goes out of
scope. Therefore I inherit from components when I make a class from which I
have slightly the idea that it has unmanaged resources in it.

http://search.microsoft.com/search/results.aspx?qu=component+class&View=msdn&st=b&c=0&s=1&swc=0

IComponent implements IDisposable for when you are searching that.

I hope this helps,

Cor
 
G

Guest

Thanks to all. I am beginning to understand a little of what the dispose is
all about and all this discussion certainly helps me and I think a lot of
people using VB.Net. I have implemented the practice of "If it's got a
dispose method, then call it when I'm thru with it and if it doesn't have a
dispose method, don't worry about it!"
 
C

Cor Ligthert

Dennis,
Thanks to all. I am beginning to understand a little of what the dispose
is
all about and all this discussion certainly helps me and I think a lot of
people using VB.Net. I have implemented the practice of "If it's got a
dispose method, then call it when I'm thru with it and if it doesn't have
a
dispose method, don't worry about it!"
While that is in my opinion not the good method. It is in a lot of cases
exactly opostite, when it has no dispose method, create it when it is using
an unmanaged resource.

When you read my message than you see that a lot of classes have dispose,
just because they inherits form component. That is not a reason to call all
the members in that which have that as well all individual.

Cor
 
G

Guest

How am I to know if objects such as OleDbDataAdaptor use unmanaged resources
and whether or not I should call it's dispose method?
 
S

Scott M.

Generally, you can look a the big picture of what the class does. With any
of the data connectivity classes (dataadapters, datareaders, connections),
calling dispose is a good idea because the basic job of these classes is to
connect to unmanaged resources.

Classes that help you get to resources "outside" of the CLR's domain are
good classes to dispose. But I wouldn't worry about disposing a label, for
example.
 
C

Cor Ligthert

Dennis,

There is no need to dispose any object of system.data.

I have written a while that the exception on this is the connection, that
is used for the connection pooling what was consequently told by Angel in
the AdoNet newsgroup and that was done something with the fysical connection
string (not the code) in a kind of overriden function from dispose.

Because this message below in that newsgroup (it is not the first) I become
confused again however keep that dispose for the connection string. The
other objects does not need a dispose. There the dispose is only because the
method is inherited from components from which all system.data classes
inherits.

http://groups-beta.google.com/group/microsoft.public.dotnet.framework.adonet/msg/7111c0671640ad38

I hope this give some idea's

Cor
 
S

Scott M.

I'm not so sure about that Cor. I have read many articles and seen many
posts that suggest that the DataAdapter, DataReader and Connection objects
SHOULD be disposed.
 
M

Michael C#

If I understand Dispose() correctly, it simply forces the .NET framework to
mark an object for Garbage Collection. As I look at the "Windows Forms
Designer generated" Dispose() method of a Form, it is appears that the Form
itself performs Dispose() on all the Form's components, which if I'm not
mistaken, would include labels, etc.

As I understand it, Dispose() does not force a Garbage Collection, so that
should not be an issue. Is there any real benefit to *not* calling
Dispose() on objects that expose it once you know that they are no longer
needed (such as a SqlCommand), or is there a complex matrix of rules for
when and when not to use Dispose() that should be referenced every time you
create a new object?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top