Dispose questions

  • Thread starter Thread starter Lance
  • Start date Start date
L

Lance

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
 
Lance said:
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.
 
* "Lance said:
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.
 
Back
Top