470,620 Members | 1,645 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,620 developers. It's quick & easy.

Dispose questions

I have a couple of dispose questions.

1. Is it advantageous to dispose Drawing.Graphics.Clip
before setting a new clip region? Here is an example:

Dim rect As New Drawing.Rectangle(10, 10, 20, 20)
Dim g As New Drawing.Graphics
g.Clip.Dispose 'Is there any advantage to this line?
g.Clip = New Drawing.Region(rect)
2. Is it necessary to dispose objects that are only
passed as parameters? Here is an example (please ignore
the fact that you could just use Graphics.FillRectangle in
this example):

Sub DrawRectUsingARegion(ByVal g As Drawing.Graphics,
ByVal rect As Drawing.Rectangle)
'Can you do the following?
g.FillRegion(New Drawing.Region(rect))
'Or, should you do this?
Dim rgn As New Drawing.Region(rect)
g.FillRegion(rgn)
rgn.Dispose
End Sub
Thanks!
Lance
Nov 20 '05 #1
3 992
* "Lance" <zi***@hotmail.com> scripsit:
1. Is it advantageous to dispose Drawing.Graphics.Clip
before setting a new clip region? Here is an example:

Dim rect As New Drawing.Rectangle(10, 10, 20, 20)
Dim g As New Drawing.Graphics
g.Clip.Dispose 'Is there any advantage to this line?
g.Clip = New Drawing.Region(rect)
AFAIS you will have to dispose this region.
2. Is it necessary to dispose objects that are only
passed as parameters? Here is an example (please ignore
the fact that you could just use Graphics.FillRectangle in
this example):

Sub DrawRectUsingARegion(ByVal g As Drawing.Graphics,
ByVal rect As Drawing.Rectangle)
'Can you do the following?
g.FillRegion(New Drawing.Region(rect))
'Or, should you do this?
Dim rgn As New Drawing.Region(rect)
g.FillRegion(rgn)
rgn.Dispose
End Sub


This depends on where the objects come from. If the caller disposes the
objects, you dont need/should not dispose them in the procedure.

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
Nov 20 '05 #2
"Lance" <zi***@hotmail.com> schrieb
I have a couple of dispose questions.

1. Is it advantageous to dispose Drawing.Graphics.Clip
before setting a new clip region? Here is an example:

Dim rect As New Drawing.Rectangle(10, 10, 20, 20)
Dim g As New Drawing.Graphics
"g As New Drawing.Graphics" does not work because there is no public
constructor.
g.Clip.Dispose 'Is there any advantage to this line?
Yes, the native region is destroyed immediatelly. Otherwise it won't be
destroyed before the region is collected and finilized by the GC.
g.Clip = New Drawing.Region(rect)
2. Is it necessary to dispose objects that are only
passed as parameters? Here is an example (please ignore
the fact that you could just use Graphics.FillRectangle in
this example):

Sub DrawRectUsingARegion(ByVal g As Drawing.Graphics,
ByVal rect As Drawing.Rectangle)
'Can you do the following?
g.FillRegion(New Drawing.Region(rect))
'Or, should you do this?
Dim rgn As New Drawing.Region(rect)
g.FillRegion(rgn)
rgn.Dispose
End Sub


I'd choose the 2nd way for the same reason given above.
--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #3
Thanks to both people who replied!
Nov 20 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by RiteshDotNet | last post: by
11 posts views Thread by Ken Durden | last post: by
6 posts views Thread by Cody Powell | last post: by
6 posts views Thread by Teresa | last post: by
14 posts views Thread by Jonas | last post: by
156 posts views Thread by Dennis | last post: by
9 posts views Thread by Geoff Callaghan | last post: by
reply views Thread by Jeff | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.