[RESOLVED] How to release memory used by an array of UDTs containing strings?

If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

[RESOLVED] How to release memory used by an array of UDTs containing strings?

What is the correct way to release all of the memory used by an array of UDTs when the UDT members contain variable length strings? Is Erase enough? Or do I need to clear out the strings in each UDT?

For example...

Code:

Private Type Person
FirstName As String
LastName As String
End Type
Private People() As Person
Private Sub Form_Load()
Dim i As Long
ReDim People(1 To 100)
For i = 1 To 100
With People(i)
.FirstName = "Joe"
.LastName = "Smith"
End With
Next
'//cleanup
'...is this necessary?
For i = 1 To 100
With People(i)
.FirstName = vbNullString
.LastName = vbNullString
End With
Next
'...or is this all I need to do?
Erase People
End Sub

Re: How to release memory used by an array of UDTs containing strings?

Yes, Erase should be enough to erase.

But the question is: what are you AAraya worried about?
Are you looking for a secure erase where the names of the persons are not left somewhere in memory after erase?
Are you worried about how much RAM memory your program uses?

Re: How to release memory used by an array of UDTs containing strings?

Originally Posted by Eduardo-

Yes, Erase should be enough to erase.

But the question is: what are you AAraya worried about?
Are you looking for a secure erase where the names of the persons are not left somewhere in memory after erase?
Are you worried about how much RAM memory your program uses?

Re: How to release memory used by an array of UDTs containing strings?

Re: How to release memory used by an array of UDTs containing strings?

I'll second (or third or fourth) that. Erase is sufficient. Just think of it as recursion or nesting. If VB6 didn't get it correct, we would have heard about it long ago. The same would be true for any Objects in your UDT array or any other sub-arrays in your UDT array.

EDIT1: The Objects will implicitly have their internal counter decremented (and un-instantiated if it went to zero).

EDIT2: The only true bug I know of when UDT arrays is that, when you pass them, the VB6 p-code interpreter and machine-code compiler don't check for "Type Mismatch". In other words, when you pass these things, they might be declared as one type of UDT and then "caught" as another type of UDT (possibly of different length). If it's not a UDT array, all works as it should (reporting Type Mismatches).

Last edited by Elroy; Feb 7th, 2018 at 09:16 AM.

Any software I post in these forums written by me is provided “AS IS” without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. Please understand that I’ve been programming since the mid-1970s and still have some of that code. My contemporary VB6 project is approaching 1,000 modules. In addition, I have a “VB6 random code folder” that is overflowing. I’ve been at this long enough to truly not know with absolute certainty from whence every single line of my code has come, with much of it coming from programmers under my employ who signed intellectual property transfers. I have not deliberately attempted to remove any licenses and/or attributions from any software. If someone finds that I have inadvertently done so, I sincerely apologize, and, upon notice and reasonable proof, will re-attach those licenses and/or attributions. To all, peace and happiness.