Results 1 to 6 of 6

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

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2011
    Location
    Palm Coast, FL
    Posts
    416

    Resolved [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

  2. #2
    Fanatic Member
    Join Date
    Feb 2017
    Posts
    858

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

    Erase should work just fine.
    To test use erase, and then see if you can access the info in the UDT.

  3. #3
    PowerPoster
    Join Date
    Feb 2017
    Posts
    4,997

    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?

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2011
    Location
    Palm Coast, FL
    Posts
    416

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

    Quote Originally Posted by Eduardo- View Post
    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?
    Sorry for being unclear in my question.

    I am looking to free up all BSTR memory used by my program.

  5. #5
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    5,872

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

    Erase is sufficient

  6. #6
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,853

    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 10: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. To all, peace and happiness.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width