Solved

Unloading An Array Of Objects

Posted on 2004-08-10
7
306 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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 utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

728 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