Freeing Dynamic Arrays

H

headware

Do you have to manually release memory allocated by creating a dynamic
array using ReDim? In other words, if I have the following code:

ReDim Test(1000)
For i = 0 To 1000
Test(i) = "test value " & i
Next

Do have I have set Test = Nothing to prevent a memory leak?

Thanks,
Dave
 
B

Bob Barrows [MVP]

headware said:
Do you have to manually release memory allocated by creating a dynamic
array using ReDim? In other words, if I have the following code:

ReDim Test(1000)
For i = 0 To 1000
Test(i) = "test value " & i
Next

Do have I have set Test = Nothing to prevent a memory leak?

No. You did not create Test using the Set keyword did you? That means it
is not considered an object and there is no need to treat it as one. If
you wish to be explicit, use

Erase Test

By the way, you seem to have gotten the idea that failing to set any
object to nothing will invariably lead to a memory leak, and this is
just not the case. With most objects, there is no problem with allowing
the vbscript garbage handler to take care of them when they go out of
scope. Where memory leaks may occur is with objects that are in a state
in which they cannot be immediately de-referenced by the garbage handler
and thus remain in memory. The most well-known culprits for this are ADO
objects that may be unable to be closed before being set to nothing,
perhaps because they may be getting handled in the wrong order: parent
objects before their child objects. It is a good idea to be explicit
with ADO objects.

You may be interested in this bit from the guy at Microsoft who was
responsible for developing a lot of the scripting runtime library:
http://blogs.msdn.com/ericlippert/archive/2004/04/28/122259.aspx
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Freeing Dynamic Arrays 1
Freeing Dynamic Arrays 2
Error freeing memory.... 14
Freeing memory 5
Organization Assignment in C programming 0
Python not freeing memory (?) 2
problem with freeing data 11
ARRAYS DOUBTS 12

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top