Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

If a Type contains a variable-length String, then the other members don't get zero'd during a ReDim _Preserve #331

Closed
mkilgore opened this issue Apr 12, 2023 · 0 comments · Fixed by #497
Assignees
Labels
bug Something isn't working

Comments

@mkilgore
Copy link
Contributor

See the below code, if you have a Type that contains a variable-length String entry along with other members and you use ReDim _Preserve to resize an array of those types, the new variable-length String's are initialize but the other members are not and contain garbage:

Type foo
    a As Long
    s As String
End Type
ReDim bar(20) As foo
bar(20).a = 20
bar(20).s = "foobar"

' This should effectively clear out the last 10 array entries
ReDim _Preserve bar(10) As foo
ReDim _Preserve bar(20) As foo

Print "bar(20).a: "; bar(20).a ' Incorrectly prints 20, it was not cleared
Print "bar(20).s: "; bar(20).s ' Correctly prints nothing, an empty string

The underlying issue is that around lines 13796 in qb64pe.bas there is separate logic for rediming an array depending on whether the type contains variable-length Strings or not. The version for Type's with variable-length strings correctly free's and initializes the strings contained in the Type, but it does not call ZeroMemory() to zero the rest of the new memory for the array. The redim logic for typical types does this zeroing, so that works correctly.

A separate point, the freeing and creation of the qbs entries seems to be quite slow. It would be nice if we could simply leave them as NULL pointers and then treat them as the empty string when they're used. That would avoid needing custom initialization logic for them and also make them more efficient by only creating the qbss if they actually get assigned.

@mkilgore mkilgore added the bug Something isn't working label Apr 12, 2023
@flukiluke flukiluke self-assigned this Jun 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants