[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 313
  • Last Modified:

Unloading An Array Of Objects

Quick question here.  I have am working in Access 2000 using vba.  I have an array of custom classes in my program.  When I am done w/ the array can I just erase the objects or do I have to loop thru the array and set each object = nothing first?

Code Example:  (How it works now)

    Dim a As Integer
    Dim Managers() As Object
        a = 1
        Do
            a = a +1
            ReDim Preserve Managers(a)
            Set Managers(a) = New ReportingLevel
        Loop Until a > 10
    Erase Managers

Just want to make sure my code is as clean as I can get it.  :-)

Thanks,
Mike
0
Shadowedvaca
Asked:
Shadowedvaca
  • 2
  • 2
  • 2
  • +1
3 Solutions
 
Carl TawnSystems and Integration DeveloperCommented:
You will have to loop the array first and delete each object, then Erase the array.
0
 
JR2003Commented:
Behind the scenes each object has a COM smart pointer.
The way COM smart pointers work is they destroy an object when nothing else references it.

So if you just erase the array all the COM objects pointed to by the array have nothing else referencing them so they destroy themselves. So your code WILL clean up after itself!

You can sometimes optimise the performance (speed) of destroying objects if you iterate through the array in the reverse order that the objects were created setting each reference to Nothing.

e.g.
   Dim i As Long
   For i = UBound(Managers) to LBound(Managers) Step -1
            Set Managers(a) = Nothing
    Next i  
    Erase Managers

NB: looping through like this is for performance only! It does not make cleaning up any better.
0
 
EDDYKTCommented:
should it be


a = 0    ' not a=1 otherwise your object will start at Managers(2)

Do
    a = a +1
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
Carl TawnSystems and Integration DeveloperCommented:
JR2003 comment isn't entirely accurate.  Objects in VB are only destroyed when every reference to the object has been deleted (i.e. explicitly set to Nothing). COM objects maintain an internal reference count (bascially telling it how many instances of an object are being used), this count is decremented each time a reference is released (i.e set to Nothing). When the reference count reaches 0 the object is destroyed.

You should always set all object variables to Nothing as a matter of good housekeeping and freeing resources when no longer needed.
0
 
ShadowedvacaAuthor Commented:
thank you guys for your explanation in the background.  Always nice to understand what I am doing.  Decided to give you all 50 points since you all helped me understand what I was doing.

Thanks,
Mike
0
 
ShadowedvacaAuthor Commented:
didn't notice that Carl posted twice.  My bad...  Thanks for the help!!!
0
 
JR2003Commented:
carl_tawn's comment isn't entirely accurate either. You do not have to explicitly set an object to nothing to destroy it. Simply going out of scope (e.g. if the object was defined inside a function) or erasing an array that points to some objects (as in your code) will be sufficient to destroy the objects and free the memory used by them.
0

Featured Post

Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

  • 2
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now