【发布时间】:2018-05-21 03:32:09
【问题描述】:
下面的代码只是尝试使用cblas_ccopy 将值从一个指针复制到另一个指针,但大约三分之一的时间会导致malloc: *** error ... incorrect checksum for freed object 错误。为什么它总是不工作?
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()
将其作为单元测试运行时,错误为objc[44736]: autorelease pool page 0x7fecb903c000 corrupted。作为脚本运行时,错误为incorrect checksum。
我尝试在malloc_error_break 中设置断点,但我不明白如何解释输出。
我还尝试将sourceArray 和destinationArray 作为参数传递给cblas_ccopy,而不将它们转换为原始指针,但这并没有帮助。
【问题讨论】:
标签: swift accelerate-framework unsafe-pointers cblas