Basic

Moderators: None (Apply to moderate this forum)
Number of threads: 1677
Number of posts: 4766

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Implementing disposable objects... Posted by Sephiroth on 12 May 2008 at 8:37 AM
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.
'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

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.

-Sephiroth
Thread Tree
Sephiroth Implementing disposable objects... on 12 May 2008 at 8:37 AM
zibadian Re: Implementing disposable objects... on 12 May 2008 at 12:06 PM
Sephiroth Re: Implementing disposable objects... on 12 May 2008 at 2:21 PM



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.