473,897 Members | 2,558 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 #1
156 5975
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 #2
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 #3
>>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.

"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 #4
What! Microsoft Wrong...I thought they were perfect!

"Bob Powell [MVP]" wrote:
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.

"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 #5
I think that spelt "perfict" ;-)

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

"Dennis" <De****@discuss ions.microsoft. com> wrote in message
news:7B******** *************** ***********@mic rosoft.com...
What! Microsoft Wrong...I thought they were perfect!

"Bob Powell [MVP]" wrote:
>>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.

"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 #6
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.

"Dennis" <De****@discuss ions.microsoft. com> wrote in message
news:7B******** *************** ***********@mic rosoft.com...
What! Microsoft Wrong...I thought they were perfect!

"Bob Powell [MVP]" wrote:
>>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.

"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 #7
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 #8
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 #9
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 #10

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.