when to call dispose()?

G

Guest

Hi,
I have a form containing controls, a timer, and the EventLog component.
I also create some new instances of classes that do not implement idisposable
I don't want to leave the tidying up to the GC so...
Do I need to explicitly call EventLog.dispose() as in the following snippet?

protected overloads sub dispose(byval disposing as boolean)
if disposing
if components is nothing then
components.dispose
end if
call MyEventLog.dispose(True) <<<--
end if
end sub

Also how do I make sure that the classes that don't implement idisposable return their memory one I've finished.Do I just close them, or set them to nothing, or what?

Help much appreciated
 
P

Palo Mraz

Do I need to explicitly call EventLog.dispose() as in the following
snippet?

Provided your Form's EventLog instance is not SHARED between
forms (i.e. it is OWNED by the Form) it is a good practice to call Dispose
during the Form's shutdown:

Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
Me.EventLog1.Dispose() <<< -
MyBase.Dispose(disposing)
End Sub

Not that you cannot call the Dispose(Boolean) method, because it is declared
as Protected in the base Component class.
Also how do I make sure that the classes that don't implement idisposable
return their memory one I've finished.Do I just close them, or set them to
nothing, or what?
In general, if a class doesn't implement IDisposable, you don't have to
worry about reclaiming the class' instances memory (by setting a reference
to Nothing, for example). Beware, however, that certain classes do NOT
expose the IDisposable.Dispose method as part of their public contract. A
canonical example being the System.IO.Stream class. In these cases, you can
cast the reference and call the Dispose method:

Dim S As System.IO.Stream = ...

' Done with S
DirectCast(S, IDisposable).Dispose()

(Usually, these classes expose some kind of a Close method that you can call
directly, e.g.: S.Close)

Please note that if you have a rooted reference to a non-IDisposable
instance whose memory you want to reclaim, setting the reference to Nothing
will allow the instance to be GC-ed.

FMI on garbage collection, see
http://msdn.microsoft.com/msdnmag/issues/1100/GCI/default.aspx.
FMI on IDisposable, see
http://msdn.microsoft.com/library/en-us/cpref/html/frlrfSystemIDisposableClassTopic.asp.
Or, from a slightly different view (WARNING: shameless selfpromotion), see
http://www.vbinfozine.com/a_disposable.shtml.

Palo
 
G

Guest

Thanks Palo - a great help
Does this mean that the only way I can reclaim memory from classes that are not IDisposable is to let the GC do it
I am concerned that I use a lot of classes and would rather release them from memory as soon as I have done with them rather than leave them for the GC at a later time

Nigel
 
P

Palo Mraz

Does this mean that the only way I can reclaim memory from classes that
are not IDisposable is to let the GC do it?
Exactly.
I am concerned that I use a lot of classes and would rather release them
from memory as soon as I have done with them rather than leave them for the
GC at a later time.
Common, relax ;-)
The GC in .NET does a pretty good job reclaiming memory so you don't have to
worry about that any more. If you're interested, try this on Google (without
quotes) "garbage collector .net framework optimized small allocations" and
you'll get a pile of good articles about the subject.
(WARNING: shameless self-promotion #2). Also, the article about my first
..NET project might be illuminative to you - see
http://www.vbinfozine.com/a_objects.shtml.

Well, if you really think you know better when to release unused memory, you
can always call GC.Collect followed by GC.WaitForPendingFinalizers. In
specific cases, it MIGHT improve performance of your app, but in general, I
wouldn't recommend that.

Palo
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top