Skip to content

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

Closed
@mkilgore

Description

@mkilgore

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.

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions