Swift arrays are value types that copy on write. If a the original array is not mutated then the “copy” points to the same memory location.
Supposed we have a class referenced by multiple threads
class Foo {
var numbers: [Int] = [1, 2, 3]
}
let foo = Foo()
If thread A “copies” numbers
var numbers = foo.numbers
and then later thread B replaces numbers
with a different array instance
foo.numbers = [4, 5, 6]
will the original numbers array ([1, 2, 3]
) be deallocated, resulting in incorrect checksum for freed object - object was probably modified after being freed
when thread B attempts to access its elements?