我创建了一个由浮点值数组填充的缓冲区。不幸的是,当我尝试查询其 contents()
属性时(当我试图调试问题时),我得到了 EXC_BAD_ACCESS。这是代码:
let inputData = [Float32](repeatElement(5, count: 16384)) // Declaration
// Declaration of the buffer and filling contents...
inputBuffer = device.makeBuffer(bytes: inputData, length: MemoryLayout<Float32>.size * inputData.count, options: MTLResourceOptions())
// Crash occurs here:
let contents = inputBuffer.contents().load(as: [Float32].self)
我想知道为什么会发生崩溃。其他类似的缓冲区声明工作正常,所以我猜这可能是访问内容的问题。
最佳答案
要使用load
方法访问数组内容,您需要指定数组元素类型(以及可选的偏移量)。例如:
let firstElement = inputBuffer.contents().load(fromByteOffset: 0, as: Float.self)
等等。您试图将第一个元素作为 [Float]
加载,这可能会解释崩溃。
关于ios - MTLBuffer 内容导致 EXC_BAD_ACCESS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44342966/