Swift vs Objective C pointer manipulation issue

送分小仙女□ 提交于 2019-12-05 11:26:52

For the call:

let buffers = UnsafeBufferPointer<AudioBuffer>(start: &listPtr.pointee.mBuffers, count: Int(listPtr.pointee.mNumberBuffers))

&listPtr.pointee.mBuffers produces a temporary pointer valid only for the duration of the call to UnsafeBufferPointer's initialiser. Therefore attempting to use the buffer pointer results in undefined behaviour (the compiler will hopefully warn on such cases in Swift 5.1).

Instead, you can iterate directly over UnsafeMutableAudioBufferListPointer, as it conforms to MutableCollection.

For example:

for index in audioBufferList.indices {
  audioBufferList[index].mDataByteSize = inNumberFrames * UInt32(sampleWordSize)
  print("Data byte size \(audioBufferList[index].mDataByteSize)")
}

for buffer in audioBufferList {
  print("Data byte size \(buffer.mDataByteSize)")
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!