473,689 Members | 2,903 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Dispose Again!

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.dosomet hingmethod(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 mydisposableobj ect as new object 2
object1.dosomet hingmethod(mydi sposableobject )
myotherobject.D ispose
--
Dennis in Houston
Nov 21 '05
156 5836
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.SuppressFina lize 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 "Implementi ng a Dispose Method" in the MSDN documentation.
"Scott M." <s-***@nospam.nosp am> wrote in message
news:%2******** ********@TK2MSF TNGP09.phx.gbl. ..
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.
"Dennis" <De****@discuss ions.microsoft. com> wrote in message
news:FF******** *************** ***********@mic rosoft.com...
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.

"Scott M." wrote:
Your second bit of code is your solution.

"Dennis" <De****@discuss ions.microsoft. com> wrote in message
news:1F******** *************** ***********@mic rosoft.com...
> 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.dosomet hingmethod(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 mydisposableobj ect as new object 2
> object1.dosomet hingmethod(mydi sposableobject )
> myotherobject.D ispose
>
>
> --
> Dennis in Houston


Nov 21 '05 #11
Dennis,

"Dennis" <De****@discuss ions.microsoft. com> schrieb:
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.dosomet hingmethod(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 mydisposableobj ect as new object 2
object1.dosomet hingmethod(mydi sposableobject )
myotherobject.D ispose


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.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #12
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/...29/271829.aspx

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

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

Hope this helps
Jay

Jay
"Bob Powell [MVP]" <bob@_spamkille r_bobpowell.net > wrote in message
news:%2******** ********@tk2msf tngp13.phx.gbl. .. 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

<<snip>>
Nov 21 '05 #13
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.
"Dennis" <De****@discuss ions.microsoft. com> wrote in message
news:FF******** *************** ***********@mic rosoft.com...
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.

"Scott M." wrote:
Your second bit of code is your solution.

"Dennis" <De****@discuss ions.microsoft. com> wrote in message
news:1F******** *************** ***********@mic rosoft.com...
> 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.dosomet hingmethod(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 mydisposableobj ect as new object 2
> object1.dosomet hingmethod(mydi sposableobject )
> myotherobject.D ispose
>
>
> --
> Dennis in Houston


Nov 21 '05 #14
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

"Jay B. Harlow [MVP - Outlook]" <Ja************ @msn.com> wrote in message
news:e5******** ********@TK2MSF TNGP12.phx.gbl. ..
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/...29/271829.aspx

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

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

Hope this helps
Jay

Jay
"Bob Powell [MVP]" <bob@_spamkille r_bobpowell.net > wrote in message
news:%2******** ********@tk2msf tngp13.phx.gbl. ..
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

<<snip>>

Nov 21 '05 #15
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.SuppressFina lize 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 "Implementi ng a Dispose Method" in the MSDN documentation.
"Scott M." <s-***@nospam.nosp am> wrote in message
news:%2******** ********@TK2MSF TNGP09.phx.gbl. ..
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.
"Dennis" <De****@discuss ions.microsoft. com> wrote in message
news:FF******** *************** ***********@mic rosoft.com...
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.

"Scott M." wrote:
Your second bit of code is your solution.

"Dennis" <De****@discuss ions.microsoft. com> wrote in message
news:1F******** *************** ***********@mic rosoft.com...
> 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.dosomet hingmethod(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 mydisposableobj ect as new object 2
> object1.dosomet hingmethod(mydi sposableobject )
> myotherobject.D ispose
>
>
> --
> Dennis in Houston


Nov 21 '05 #16
Dennis,

"Dennis" <De****@discuss ions.microsoft. com> schrieb:
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.dosomet hingmethod(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 mydisposableobj ect as new object 2
object1.dosomet hingmethod(mydi sposableobject )
myotherobject.D ispose


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.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #17
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/r...&c=0&s=1&swc=0

IComponent implements IDisposable for when you are searching that.

I hope this helps,

Cor
Nov 21 '05 #18
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/r...&c=0&s=1&swc=0

IComponent implements IDisposable for when you are searching that.

I hope this helps,

Cor
Nov 21 '05 #19
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!"

"Cor Ligthert" wrote:
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/r...&c=0&s=1&swc=0

IComponent implements IDisposable for when you are searching that.

I hope this helps,

Cor

Nov 21 '05 #20

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

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.