Solved

Unloading An Array Of Objects

Posted on 2004-08-10
7
303 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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…

770 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