PC Review


Reply
Thread Tools Rate Thread

Dispose Again!

 
 
=?Utf-8?B?RGVubmlz?=
Guest
Posts: n/a
 
      18th Mar 2005
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


--
Dennis in Houston
 
Reply With Quote
 
 
 
 
Scott M.
Guest
Posts: n/a
 
      18th Mar 2005
Your second bit of code is your solution.

"Dennis" <(E-Mail Removed)> wrote in message
news:1F1E1B11-425A-4C5F-9C3F-(E-Mail Removed)...
> 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
>
>
> --
> Dennis in Houston



 
Reply With Quote
 
=?Utf-8?B?RGVubmlz?=
Guest
Posts: n/a
 
      18th Mar 2005
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" <(E-Mail Removed)> wrote in message
> news:1F1E1B11-425A-4C5F-9C3F-(E-Mail Removed)...
> > 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
> >
> >
> > --
> > Dennis in Houston

>
>
>

 
Reply With Quote
 
Bob Powell [MVP]
Guest
Posts: n/a
 
      18th Mar 2005
>>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" <(E-Mail Removed)> wrote in message
news:FF6483BE-7577-4991-98EE-(E-Mail Removed)...
>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" <(E-Mail Removed)> wrote in message
>> news:1F1E1B11-425A-4C5F-9C3F-(E-Mail Removed)...
>> > 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
>> >
>> >
>> > --
>> > Dennis in Houston

>>
>>
>>



 
Reply With Quote
 
=?Utf-8?B?RGVubmlz?=
Guest
Posts: n/a
 
      18th Mar 2005
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" <(E-Mail Removed)> wrote in message
> news:FF6483BE-7577-4991-98EE-(E-Mail Removed)...
> >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" <(E-Mail Removed)> wrote in message
> >> news:1F1E1B11-425A-4C5F-9C3F-(E-Mail Removed)...
> >> > 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
> >> >
> >> >
> >> > --
> >> > Dennis in Houston
> >>
> >>
> >>

>
>
>

 
Reply With Quote
 
Bob Powell [MVP]
Guest
Posts: n/a
 
      18th Mar 2005
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" <(E-Mail Removed)> wrote in message
news:7B6E7173-3D97-423E-AC11-(E-Mail Removed)...
> 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" <(E-Mail Removed)> wrote in message
>> news:FF6483BE-7577-4991-98EE-(E-Mail Removed)...
>> >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" <(E-Mail Removed)> wrote in message
>> >> news:1F1E1B11-425A-4C5F-9C3F-(E-Mail Removed)...
>> >> > 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
>> >> >
>> >> >
>> >> > --
>> >> > Dennis in Houston
>> >>
>> >>
>> >>

>>
>>
>>



 
Reply With Quote
 
Bob Powell [MVP]
Guest
Posts: n/a
 
      18th Mar 2005
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" <(E-Mail Removed)> wrote in message
news:7B6E7173-3D97-423E-AC11-(E-Mail Removed)...
> 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" <(E-Mail Removed)> wrote in message
>> news:FF6483BE-7577-4991-98EE-(E-Mail Removed)...
>> >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" <(E-Mail Removed)> wrote in message
>> >> news:1F1E1B11-425A-4C5F-9C3F-(E-Mail Removed)...
>> >> > 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
>> >> >
>> >> >
>> >> > --
>> >> > Dennis in Houston
>> >>
>> >>
>> >>

>>
>>
>>



 
Reply With Quote
 
Jay B. Harlow [MVP - Outlook]
Guest
Posts: n/a
 
      18th Mar 2005
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@_spamkiller_bobpowell.net> wrote in message
news:%(E-Mail Removed)...
> 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>>


 
Reply With Quote
 
Scott M.
Guest
Posts: n/a
 
      18th Mar 2005
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" <(E-Mail Removed)> wrote in message
news:FF6483BE-7577-4991-98EE-(E-Mail Removed)...
>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" <(E-Mail Removed)> wrote in message
>> news:1F1E1B11-425A-4C5F-9C3F-(E-Mail Removed)...
>> > 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
>> >
>> >
>> > --
>> > Dennis in Houston

>>
>>
>>



 
Reply With Quote
 
Scott M.
Guest
Posts: n/a
 
      18th Mar 2005
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]" <(E-Mail Removed)> wrote in message
news:e5u%(E-Mail Removed)...
> 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@_spamkiller_bobpowell.net> wrote in message
> news:%(E-Mail Removed)...
>> 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>>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Does DataGridView.Dispose Call DataGridViewRow.Dispose? eBob.com Microsoft VB .NET 13 31st Jul 2009 02:08 PM
Dispose Pattern -- why void Dispose() is nonvirtual? gz Microsoft Dot NET 2 24th Jan 2008 08:18 PM
Dispose, Dispose(true), Finalize, IDisposable tascien@gmail.com Microsoft Dot NET 1 27th Jul 2007 01:01 AM
Form.Dispose does not invoke InputPanel.Dispose tomj@softhome.net Microsoft Dot NET Compact Framework 3 24th Jan 2006 03:28 PM
the difference betweenSqlConnection.IDisposable.Dispose() andSqlConnection.Dispose(). tangyong Microsoft Dot NET Framework 1 20th Jan 2006 04:53 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 05:55 PM.