Solved

Unloading An Array Of Objects

Posted on 2004-08-10
7
302 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

911 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now