When I allocate an array and copy it to another array by ref - everything works just fine. But if I add an array to a dictionary and try to copy it by reference, the result array will have incorrect boundaries and trying to do any action with the new variable will make the Excel crash.
Sub Works()
Dim a() As Variant
ReDim a(2, 2)
a(0, 0) = 1
Dim b As Variant
b = GetArrayByRef(a)
b(1, 1) = 4
End Sub
Sub ExcelCrashes()
Dim a() As Variant
ReDim a(2, 2)
a(0, 0) = 1
Dim dict As New Dictionary
dict.Add 0, a
Dim b As Variant
b = GetArrayByRef(dict(0)) ' The memory is not allocated properly here.
b(1, 1) = 4 ' Excel crash.
End Sub
When I allocate an array and copy it to another array by ref - everything works just fine. But if I add an array to a dictionary and try to copy it by reference, the result array will have incorrect boundaries and trying to do any action with the new variable will make the Excel crash.