dcsimg
Results 1 to 6 of 6

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

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2011
    Posts
    188

    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
    Addicted Member
    Join Date
    Feb 2017
    Posts
    231

    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
    Fanatic Member
    Join Date
    Feb 2017
    Posts
    986

    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?
    vbExtra: Print preview for VB6, print FlexGrids and more.
    MSDN online for VB6, Language reference.

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Aug 2011
    Posts
    188

    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
    Join Date
    Oct 2013
    Posts
    3,148

    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
    4,534

    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. 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.

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
  •  



Featured


Click Here to Expand Forum to Full Width