The code below simply tries to copy values from one pointer to another, using cblas_ccopy
, but it results in an malloc: *** error ... incorrect checksum for freed object
error about one third of the time. Why doesn't it always work?
import Accelerate
func testCopy() {
// set capacity
let capacity: Int = 1000
// destination array
let destinationArray = UnsafeMutablePointer<Float>.allocate(capacity: capacity)
destinationArray.initialize(repeating: 0, count: capacity)
// source array
let sourceArray = UnsafeMutablePointer<Float>.allocate(capacity: capacity)
sourceArray.initialize(repeating: 1, count: capacity)
// copy values
cblas_ccopy(Int32(capacity),
UnsafeRawPointer(sourceArray),
1,
UnsafeMutableRawPointer(destinationArray),
1)
// check to see if values were copied
for idx in 0..<capacity {
print(idx, destinationArray[idx])
}
}
testCopy()
When running this as a unit test, the error is objc[44736]: autorelease pool page 0x7fecb903c000 corrupted
. When running it as a script, the error is incorrect checksum
.
I tried setting a breakpoint in malloc_error_break
but I don't understand how to interpret the output.
I also tried passing sourceArray
and destinationArray
to cblas_ccopy
as arguments, without converting them to raw pointers, but that did not help.
Use cblas_scopy
instead of cblas_ccopy
. cblas_ccopy
copies (single precision) complex numbers which are twice the size of the single precision numbers you actually are using, so you're overrunning the end of the buffer.