Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Implementing disposable objects...

SephirothSephiroth Fayetteville, NC, USAMember Posts: 1,035
Alright, I am creating three objects in my latest project that will have allocated arrays and such during their usage, and I want to implement the "Dispose()" method for clean-up. I have already implements the proper objects, but I am curious about some of what was automatically inserted into my object code.
[code]
'The module object
Public Class moduleClass
'Object implementations
Implements IDisposable

'Global objects
Private disposedValue As Boolean = False
Private nameString As String
Private objectCountULong As ULong
Private objectArray() As objectClass

' IDisposable
Protected Overridable Sub Dispose(ByVal disposing As Boolean)
If Not Me.disposedValue Then
If disposing Then
' TODO: free unmanaged resources when explicitly called
End If

' TODO: free shared unmanaged resources
End If
Me.disposedValue = True
End Sub

#Region " IDisposable Support "
' This code added by Visual Basic to correctly implement the disposable pattern.
Public Sub Dispose() Implements IDisposable.Dispose
' Do not change this code. Put cleanup code in Dispose(ByVal disposing As Boolean) above.
Dispose(True)
GC.SuppressFinalize(Me)
End Sub
#End Region

End Class
[/code]
That is the beginning of my object, and the stuff that was inserted at the end of the code. I first want to know if I need the region tags, because I'll remove them if I don't. The next thing is what the heck is it putting two Dispose() methods in the class for, and why does one have a boolean value passed to it? Seems like a waste of code if I can simply plop everything into one method. Finally, what is the difference in the two resources that the first Dispose() method mentions? All I want to do is free some arrays when the object is deleted, freed, or whatever.

*EDIT*

Actually, I may not need to perform any cleanup. If I declare an array like "Private myArrayInt() As Integer" and then use "ReDim" to change the size while using it, will I need to use "ReDim myArrayInt(-1)" during cleanup, or will the framework automatically free this stuff? I am a C/C++ programmer used to using new/delete and malloc/free, so this VB idea of never allocating or releasing resources seems far-fetched to me. It just seems like the framework can't possibly know every array or variable that you allocate and properly free it when your application exits.

-[italic][b][red]S[/red][purple]e[/purple][blue]p[/blue][green]h[/green][red]i[/red][purple]r[/purple][blue]o[/blue][green]t[/green][red]h[/red][/b][/italic]

Comments

  • zibadianzibadian Member Posts: 6,349
    : Alright, I am creating three objects in my latest project that will
    : have allocated arrays and such during their usage, and I want to
    : implement the "Dispose()" method for clean-up. I have already
    : implements the proper objects, but I am curious about some of what
    : was automatically inserted into my object code.
    : [code]:
    : 'The module object
    : Public Class moduleClass
    : 'Object implementations
    : Implements IDisposable
    :
    : 'Global objects
    : Private disposedValue As Boolean = False
    : Private nameString As String
    : Private objectCountULong As ULong
    : Private objectArray() As objectClass
    :
    : ' IDisposable
    : Protected Overridable Sub Dispose(ByVal disposing As Boolean)
    : If Not Me.disposedValue Then
    : If disposing Then
    : ' TODO: free unmanaged resources when explicitly called
    : End If
    :
    : ' TODO: free shared unmanaged resources
    : End If
    : Me.disposedValue = True
    : End Sub
    :
    : #Region " IDisposable Support "
    : ' This code added by Visual Basic to correctly implement the disposable pattern.
    : Public Sub Dispose() Implements IDisposable.Dispose
    : ' Do not change this code. Put cleanup code in Dispose(ByVal disposing As Boolean) above.
    : Dispose(True)
    : GC.SuppressFinalize(Me)
    : End Sub
    : #End Region
    :
    : End Class
    : [/code]:
    : That is the beginning of my object, and the stuff that was inserted
    : at the end of the code. I first want to know if I need the region
    : tags, because I'll remove them if I don't. The next thing is what
    : the heck is it putting two Dispose() methods in the class for, and
    : why does one have a boolean value passed to it? Seems like a waste
    : of code if I can simply plop everything into one method. Finally,
    : what is the difference in the two resources that the first Dispose()
    : method mentions? All I want to do is free some arrays when the
    : object is deleted, freed, or whatever.
    :
    : *EDIT*
    :
    : Actually, I may not need to perform any cleanup. If I declare an
    : array like "Private myArrayInt() As Integer" and then use "ReDim" to
    : change the size while using it, will I need to use "ReDim
    : myArrayInt(-1)" during cleanup, or will the framework automatically
    : free this stuff? I am a C/C++ programmer used to using new/delete
    : and malloc/free, so this VB idea of never allocating or releasing
    : resources seems far-fetched to me. It just seems like the framework
    : can't possibly know every array or variable that you allocate and
    : properly free it when your application exits.
    :
    : -[italic][b][red]S[/red][purple]e[/purple][blue]p[/blue][green]h[/gre
    : en][red]i[/red][purple]r[/purple][blue]o[/blue][green]t[/green][red]h
    : [/red][/b][/italic]

    The Object class has the Dispose() method defined as being:
    [code]
    Protected Overridable Sub Dispose(ByVal disposing As Boolean)
    [/code]
    If you want to add your code to dispose of custom fields, then you need to override this method.
    The second Dispose() (the one without any parameters) comes from the IDisposable interface. This method can be called by some VB framework (I don't know which) do automatically dispose of the registered objects. Since this framework expects a Dispose() without any parameters, your object must implement that one as well.
    Because the actual implementation is in the Dispose(Boolean) method, the Dispose() method simply calls that method, and should not do anything else. The other statement in the Dispose() notifies the garbage collector, that this object is free for collection.
  • SephirothSephiroth Fayetteville, NC, USAMember Posts: 1,035
    : The Object class has the Dispose() method defined as being:
    : [code]:
    : Protected Overridable Sub Dispose(ByVal disposing As Boolean)
    : [/code]:
    : If you want to add your code to dispose of custom fields, then you
    : need to override this method.
    : The second Dispose() (the one without any parameters) comes from the
    : IDisposable interface. This method can be called by some VB
    : framework (I don't know which) do automatically dispose of the
    : registered objects. Since this framework expects a Dispose() without
    : any parameters, your object must implement that one as well.
    : Because the actual implementation is in the Dispose(Boolean) method,
    : the Dispose() method simply calls that method, and should not do
    : anything else. The other statement in the Dispose() notifies the
    : garbage collector, that this object is free for collection.

    Alright, in that case I don't need to use Dispose on an object that only contains an array of a default type, such as String. Is this correct? What about an object that contains an object I created like below.
    [code]
    Class ObjectA
    Private intArray(64) As Integer
    Private strArray(128) As String
    End Class

    Class ObjectB
    Private customArray(32) As ObjectA
    End Class
    [/code]
    Would either of those need to implement a dispose method? This is essentially what I am doing. I have a master class that contains a string and an array of another class I created. That second class contains a string and an array of a third class I created. The third class contains nothing but stock variable-types, such as int, long, string, etc. If I don't have to implement disposal, I'll be able to finish the project sooner.

    -[italic][b][red]S[/red][purple]e[/purple][blue]p[/blue][green]h[/green][red]i[/red][purple]r[/purple][blue]o[/blue][green]t[/green][red]h[/red][/b][/italic]
Sign In or Register to comment.