Roland,
In addition to the other comments: I would use a variation of your first
method:
.NET 1.x
Dim fnt as Font = New Font(...)
Try
DrawString(myText, fnt,...)
Finally
' assuming fnt is not nothing
fnt.dispose().
End Try
.NET 2.0
Using fnt as Font = New Font(...)
DrawString(myText, fnt,...)
End Using
As this ensure that any unmanaged resources that Font may be exposing (as
evidenced by the fact that Font implements IDisposable) are released in a
timely manner. As you have pointed out. The GC may take minutes or longer
to
release the resource, this may cause undue pressure on both the GC &
Win32.
The GC as it is tracking a number of Finalizable objects, Win32 as you are
using Handles longer then necessary.
When to call Dispose?
* Call Dispose when the type itself implements IDisposable
* Call Dispose when the type or one of its base classes *overrides*
Dispose(Boolean) if the class inherits from
System.ComponentModel.Component
or System.ComponentModel.MarshalByValueComponent
* Do not explicitly call Dispose on classes deriving from
System.Windows.Forms.Control for instances placed on a
System.Windows.Forms.Form as Form will implicitly dispose of them when the
form is Disposed
* Call Dispose on System.Windows.Forms.Form objects when Form.ShowDialog
is
used.
* Do not explicitly call Dispose on System.Windows.Forms.Form objects if
Form.Show is used as Dispose will be implicitly called when the form is
closed
* Do not explicitly call Dispose on classes deriving from
System.Web.UI.Control as it will be implicitly called as part of the
normal
ASP.NET page processing
I consider the second rule controversial as it relies on using ILDASM or
Reflector to find out implementation details of a class. Its meant for
classes such as DataSet, that have an inherited Dispose, but Dispose
doesn't
really do anything.
These rules are based on private discussions with other MVPs & discussions
held in the newsgroups earlier in 2005.
These rules apply to objects that you create, explicitly or implicitly.
Objects that you "own".
Disposable Objects that are passed to you as a parameter of a method (such
as the Graphics object on the Paint event) should not have their disposed
method call, as the system calls it as part of the method that raises the
event.
Objects that something else "owns" should normally be disposed of by the
"owning" object.
--
Hope this helps
Jay B. Harlow [MVP - Outlook]
.NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley -
http://www.tsbradley.net
| The following issue is puzzling me:
|
| There are 2 ways of writing the code below:
| ...
| Dim fnt as Font = New Font(...)
| DrawString(myText, fnt,...)
| fnt.dispose().
|
| or
| DrawString(myText, New Font(...),...)
|
| The difference between both is that in the second case, there is no way
to
| explicitely dispose of the object (in the example, a Font object, but
that
| is purely circumstantial). The basic issue is:
|
| If an object is substantiated while passed a a parameter (as above), is
it
| inherently disposed by the called procedure (here: drawstring) or isn't
it
| at all?
|
| The reason for this question is that I favor using the second way of
coding,
| because it much less verbose, but I am not sure about the consequences.
|
| Can anybody shed some light on that? Thanks in advance.
|
|