Do I need to destroy a collection I made when I am done with i
All procedure level objects and varaibles are stored on the stack. When the
procedure ends the objects and variables are removed from the stack. Think of
it like building blocks where you can add and remove block. The blocks in
this case are memory blocks. When a procedure initializes it creates the
blocks. When the procedure ends the blocks are taken off of the stack. The
reason that it is considered to be good coding practice to destroy the
objects is that the entire object is not stored on the stack. There is just a
pointer to a different memory address where the object resides. When the
procedure ends and the pointer is destroyed the actual object is also
supposed to be destroyed. If for some reason the object is not destroyed then
you have a memory leak. You have an object in memory with absolutely no way
to access it. In practice I don't tend to set my objects back to nothing when
I am done. I trust that the program destroys the
Note that this is different from Heap memory where your constants and
globals are stored. That is similar to the stack but the memory blocks are
added and removed when the program starts and ends.
--
HTH...
Jim Thomlinson
"RyanH" wrote:
Cool! So my collection is destroyed when the procedure ends? If I need to
destroy a variable, how do I do that?
Kill VariableName?
--
Cheers,
Ryan
"Alex Simmons" wrote:
On Sep 3, 1:34 pm, RyanH wrote:
I have some code that intializes a new collection, runs code using the
collection and then ends. Do I need to destroy the collection so it doesn't
take up memory or does this automatically happen when the procedure is over?
Is there a way to see what is in memory?
Option Explicit
Private Sub cboMounting_Change()
Dim colPoleSpecsAll As Collection
Dim colPoleSpecsData As Collection
Dim ctrl As Control
Set colPoleSpecsAll = New Collection
With colPoleSpecsAll
.Add lblPoles
.Add cboPoles
.Add lblShape
.Add cboShape
.Add lblFieldPoleSize
.Add cboFieldPoleSize
End With
Set colPoleSpecsData = New Collection
With colPoleSpecsData
.Add cboPoles
.Add cboShape
.Add cboFieldPoleSize
End With
If cboMounting.ListIndex 1 Then
For Each ctrl In colPoleSpecsAll
ctrl.Enabled = True
Next ctrl
For Each ctrl In colPoleSpecsData
ctrl.BackColor = vbWindowBackground
Next ctrl
Else
For Each ctrl In colPoleSpecsAll
ctrl.Enabled = False
Next ctrl
For Each ctrl In colPoleSpecsData
ctrl.BackColor = vbButtonFace
Next ctrl
End If
End Sub
--
Cheers,
Ryan
Procedure level variables are destroyed when the procedure finishes,
however it's often thought of as good practice to destroy any objects
as soon as they become redundant - this is especially the case when
there's error handling involved etc.
The locals window in the VBE will tell you if your variables are in
use or not.
|