In general, any code that performs a direct assignment after setting the capacity of a variable (and still relies on the previous capacity or address) is suspect. In this case you can see that a single-character value is being assigned, but in general, a direct assignment may free the variable's previous memory block and invalidate any assumptions about capacity. This can happen even when assigning a non-empty string to a variable which has sufficient capacity, if the value being assigned is known to be allocated with malloc and would otherwise have to be immediately freed (it's done to avoid unnecessary copying). For example:
Code: Select all
VarSetCapacity(x, 1000, 0)
s := ""
s .= "a"
MsgBox % VarSetCapacity(x) ; 1000
x := SubStr(s, 1, -1) ; SubStr allocates memory to return the substring.
MsgBox % VarSetCapacity(x) ; 512 (if Unicode)
; Tested on v1.1.32.00
will not have this effect because in that case, SubStr can just return a pointer into the original string. After SubStr returns, the expression evaluator copies the result (unless the expression is ending and the result is being discarded).