Solved

Unloading An Array Of Objects

Posted on 2004-08-10
7
304 Views
Last Modified: 2010-05-02
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
Comment
Question by:Shadowedvaca
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 52

Assisted Solution

by:Carl Tawn
Carl Tawn earned 100 total points
ID: 11765622
You will have to loop the array first and delete each object, then Erase the array.
0
 
LVL 18

Assisted Solution

by:JR2003
JR2003 earned 50 total points
ID: 11766457
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
 
LVL 26

Expert Comment

by:EDDYKT
ID: 11766583
should it be


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

Do
    a = a +1
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 52

Accepted Solution

by:
Carl Tawn earned 100 total points
ID: 11766811
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
 

Author Comment

by:Shadowedvaca
ID: 11767249
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
 

Author Comment

by:Shadowedvaca
ID: 11767260
didn't notice that Carl posted twice.  My bad...  Thanks for the help!!!
0
 
LVL 18

Expert Comment

by:JR2003
ID: 11770423
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question