Re: Confused about GC & Disposing: How to get it right?

From: Alvin Bruney [MVP] (vapor)
Date: 11/08/04


Date: Mon, 8 Nov 2004 11:14:09 -0600

Get Richter's book. It will teach you a lot more than just disposing. In
fact, you should have it in your library anyway.

-- 
Regards,
Alvin Bruney
[ASP.NET MVP http://mvp.support.microsoft.com/default.aspx]
Got tidbits? Get it here... http://tinyurl.com/27***
"Conceptor" <Conceptor@discussions.microsoft.com> wrote in message 
news:667F3808-46CB-45CD-A1D0-0307E43952FD@microsoft.com...
> Hi,
>
>  I have read alot on Disposing and Garbage collecting and I am still
> confused about how to get it right.  I use Frameword 1.1, VB.Net and I 
> need
> to understand the best practices concerning the following issues.
>
>  Let's start at the beginning: I need to know what to do when I have a
> class that uses instances of objects that have the dispose method, let's 
> say
> third party Menu objects.
>
> 1) Should I bother about disposing at all?  I read that I should, but a
> confirmation would be great.  Instinctively, I would rather develop an
> application that disposes of the objects it creates as soon as possible. 
> Am
> I right in assuming this?
>
> 2) Should I use the Dispose Design Pattern on my class to dispose of a 
> Menu
> object?  Should I conclude that every class that owns resources that have 
> the
> dispose method should also use the Dispose Design Pattern?  Or should I
> dispose of the menu instances directly in the finalize instead?
>
> 3) Could you give an example of a typical class that uses Disposable 
> object
> instances that would teach me how I should dispose of them?
>
> 4) As for the Dispose Design Pattern, what is the difference between 
> managed
> and managed resources? Is there some rule of thumb I should be using to
> differenciate between the two?  Is it that Unmanaged resources have a 
> dispose
> method and Managed resources don't?
> In the Dispose Design Pattern, there is this method:
>   Protected Overloads Overridable Sub Dispose(disposing As Boolean)
>      ' Check to see if Dispose has already been called.
>      If Not (Me.disposed) Then
>         ' If disposing equals true, dispose all managed and unmanaged
> resources.
>         If (disposing) Then
>            ' Dispose managed resources.
>         End If
>         ' Release unmanaged resources.
>         Me.disposed = true
>      End If
>   End Sub
> Where would I call
> a) myMenu.dispose
> b) myArrayList.clear
> c) myDataSet.dispose and/or myDataSet.Close
>
> 5) I use ArrayLists in my classes.  These ArrayLists may contain objects
> that have a disposable method.  How and where should I dispose of them? 
> Will
> calling the clear method dispose of these objects for me?  I have created 
> an
> ArrayList subclass that looks like this:
>   Public Class DisposableArrayList
>      Inherits ArrayList
>      Protected Overrides Sub Finalize()
>         Try
>            Dispose(False)
>         Catch
>            'TODO: Send to console
>         Finally
>            MyBase.Finalize()
>         End Try
>      End Sub
>      Public Overloads Sub Dispose() Implements System.IDisposable.Dispose
>         Dispose(True)
>         GC.SuppressFinalize(Me)
>      End Sub
>      Protected Overridable Overloads Sub Dispose(ByVal disposing As 
> Boolean)
>         If Not (_Disposed) Then
>            If (disposing) Then
>               DisposeItems()
>            End If
>
>            _Disposed = True
>         End If
>      End Sub
>      public Sub DisposeItems()
>         Dim item As Object
>         Dim disposableItem As System.IDisposable
>         Dim iterator As System.Collections.IEnumerator
>         Dim oef As Boolean
>
>         iterator = MyBase.GetEnumerator
>
>         Do
>            oef = Not iterator.MoveNext()
>
>            If oef Then
>               Exit Do
>            Else
>               item = iterator.Current
>
>               If TypeOf item Is System.IDisposable Then
>                  disposableItem = CType(item, System.IDisposable)
>                  disposableItem.Dispose()
>               End If
>            End If
>         Loop
>
>         MyBase.Clear()
>
>         iterator = Nothing
>         disposableItem = Nothing
>         item = Nothing
>      End Sub
>   End Class
> Is that a good way to do it?
>
> Thanks for de-confusing me up,
>
> E. Thouin 

Loading