After update XCode to 15, I encountered a crash with UnsafeMutableRawPointer. To recreate the problem I write this simple test code.
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
test()
}
private func test() {
var abl = AudioBufferList()
let capacity = 4096
let lp1 = UnsafeMutableAudioBufferListPointer(&abl)
let outputBuffer1 = UnsafeMutablePointer<Int8>.allocate(capacity: capacity)
let outputBuffer2 = UnsafeMutablePointer<Int8>.allocate(capacity: capacity)
// It crashed here
lp1[0].mData = UnsafeMutableRawPointer(outputBuffer1)
lp1[0].mNumberChannels = 1
lp1[0].mDataByteSize = UInt32(capacity)
lp1[1].mData = UnsafeMutableRawPointer(outputBuffer2)
lp1[1].mNumberChannels = 1
lp1[1].mDataByteSize = UInt32(capacity)
let lp2 = UnsafeMutableAudioBufferListPointer(&abl)
let data = (
UnsafeMutablePointer<Int16>.allocate(capacity: 4096),
packet: 1
)
lp2[0].mData = UnsafeMutableRawPointer(data.0)
}
}
I checked the XCode 15 Release Notes and found out they did something with the pointer default initialization
P1020R1 - Smart pointer creation with default initialization
Is this causing the problem or I'm doing it wrong? Because it work perfectly fine with XCode 14.3.1 and below
P/s: I can't provide the full crash logs cause it's company property but I can provide these:
Exception Type: EXC_CRASH (SIGABRT)
Exception Codes: 0x0000000000000000, 0x0000000000000000
Exception Note: EXC_CORPSE_NOTIFY
Triggered by Thread: 5
Application Specific Information:
stack buffer overflow
Thread 5 name: Dispatch queue: com.apple.NSXPCConnection.user.endpoint
Thread 5 Crashed:
0 libsystem_kernel.dylib 0x20419ab78 __pthread_kill + 8
1 libsystem_pthread.dylib 0x23de0c3bc pthread_kill + 268
2 libsystem_c.dylib 0x1d780c44c __abort + 128
3 libsystem_c.dylib 0x1d77f7868 __stack_chk_fail + 96
Clearly there are something wrong with the memory address after init UnsafeMutableRawPointer
from the UnsafeMutablePointer<Int8>