By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,560 Members | 1,070 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,560 IT Pros & Developers. It's quick & easy.

After using an array, is it something to nothing?

P: n/a
MLH
I get the concept of setting an object variable
lilke rstCustomers to Nothing (Set rstCustomers = Nothing)

But what about other variable types that can consume
sizable chunks of memory. For instance, array variables.
Is memory automatically released when the called procedure
utilizing them goes out-a-scope? Do programmers have to
worry about memory leaks that may/might/dunno occur?
Oct 11 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
huh? You only set objects = Nothing. Other variables just get
destroyed when they go out of scope.

Oct 11 '06 #2

P: n/a
MLH wrote:
For instance, array variables.
Is memory automatically released when the called procedure
utilizing them goes out-a-scope?
Not for dynamic arrays. Use:

Erase ArrayName

before ArrayName goes out of scope. Read the help file for Erase and ReDim.
Do programmers have to
worry about memory leaks that may/might/dunno occur?
Yes, but there's only so much you can do about it in VBA. You know to close
objects you open and then set them to Nothing, and now you know you should
erase dynamic arrays, too. That's about it.

--
Message posted via AccessMonster.com
http://www.accessmonster.com/Uwe/For...ccess/200610/1

Oct 12 '06 #3

P: n/a

Granny Spitz via AccessMonster.com wrote:
Not for dynamic arrays. Use:

Erase ArrayName
We can use Erase for dimensioned arrays as well as dynamic arrays,
although this seems not to be documented.

We might use this if we have an array of objects, for example an array
of a specific form in a case where we wanted to open more than one
instance of that form.

Dim ArrayOfSpecificForm(0 To 9) As Form_Employees

Public Sub birth()
Dim z As Long
For z = LBound(ArrayOfSpecificForm) To UBound(ArrayOfSpecificForm)
Set ArrayOfSpecificForm(z) = New Form_Employees
With ArrayOfSpecificForm(z)
.Visible = True
.Caption = "Instance " & z
End With
Next z
End Sub

Public Sub death()
Erase ArrayOfSpecificForm
MsgBox UBound(ArrayOfSpecificForm)
MsgBox ArrayOfSpecificForm(0) Is Nothing
End Sub

Erasing the dimensioned array has the same effect as looping through
the array, closing each form and setting the array element to nothing.
The array still has ten elements after calling Erase, but they are all
nothing. It is reusable, that is the first Sub can be run again.
The MsgBox calls are only to illustrate the nature of the array after
calling Erase.

Note that the Employees from must have a module or the HasModule
property set to true in order to use Form_Employees.

One can experiment with Erase ... as below and beyond:

Sub f()
Dim a(0 To 1) As String
a(0) = "Granny"
a(1) = "Spitz"
Debug.Print Len(a(0))
Debug.Print Len(a(1))
Erase a
Debug.Print Len(a(0))
Debug.Print Len(a(1))
End Sub

Sub g()

Dim a(0 To 1) As Double
a(0) = 5 / 3
a(1) = 12 / 7
Debug.Print a(0)
Debug.Print a(1)
Erase a
Debug.Print a(0)
Debug.Print a(1)

End Sub

Oct 12 '06 #4

P: n/a
Granny Spitz via AccessMonster.com wrote:
MLH wrote:
>>For instance, array variables.
Is memory automatically released when the called procedure
utilizing them goes out-a-scope?


Not for dynamic arrays. Use:

Erase ArrayName

before ArrayName goes out of scope. Read the help file for Erase and ReDim.

>>Do programmers have to
worry about memory leaks that may/might/dunno occur?


Yes, but there's only so much you can do about it in VBA. You know to close
objects you open and then set them to Nothing, and now you know you should
erase dynamic arrays, too. That's about it.
Also, closing Recordsets is really really important to prevent memory leaks.

--
'---------------
'John Mishefske
'---------------
Oct 12 '06 #5

P: n/a
MLH
I thought I'd heard something about
other sources for memory leaks & the
like. Thx. Will look N2 it. Could not
recall Erase ArrayName =gettin' too
old for this stuff, I swear.
Oct 13 '06 #6

P: n/a
"Granny Spitz via AccessMonster.com" <u26473@uwewrote in
news:67a28a69794af@uwe:
MLH wrote:
>Do programmers have to
worry about memory leaks that may/might/dunno occur?

Yes, but there's only so much you can do about it in VBA. You know to
close objects you open and then set them to Nothing, and now you know
you should erase dynamic arrays, too. That's about it.
Yeah, I spend hours evry day worrying about West Nile Virus, Bird 'Flu,
Adolph Harper and Aids, oh, and not closing database objects and
releasing pointers to them.

So far none of these has ever done me the slightest bit of harm. But I
guess it's fashionable to be safe: oh look Mommie, see where it says,
"Rcs.Close, Set Rcs = Nothing." Aren't I a good boy, Mommie? Aren't I?

Have you ever thought about the statement
Set Rcs = Nothing? What is Nothing? And how could anything be equal to
nothing? Huh? Well, upon further review we find this statement simply
sets the value of the object pointer to zero. So what's "Set Rcs =
Nothing" got to do with that?

I prefer this:

1. don't explicitly open dao recordsets ever;
2. don't open ado recordsets very often; limit this to twice a year;
3. don't close anything;
4. don't release anything;
5. when something doesn't work; repair it;
6. don't write 50000 lines of unnecessary code; this will make #5 way
easier.

I know the MVPs and David won't agree with me (those who haven't
demonstrated their tolerance for disagreement by plonking me that is).
So, what more evidence do you want?

--
Lyle Fairfield
Oct 13 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.