Post

Replies

Boosts

Views

Activity

Reply to Bad access with a fixed size array and index in range
I'm going to call this one fixed now, thanks for the help @Claude31. Here's the amended code for the CrossingBuffer class: class CrossingBuffer { private var array: [ZeroCrossing] // NSLock Added var lock = NSLock() private let size: Int private var nextWriteIndex = 0 private var full: Bool { nextWriteIndex >= size } init(size: Int) { self.size = size array = [ZeroCrossing](repeating: ZeroCrossing(index: 0, previousPeak: 0, indexWithOffset: 0), count: size) array.reserveCapacity(size) } public func write(_ val: ZeroCrossing) { //Lock before write lock.lock() array[nextWriteIndex % size] = val //Unlock lock.unlock() nextWriteIndex += 1 } public func getAfterIndex(_ refIndex: Double) -> [ZeroCrossing]? { if !full { return nil } var subArray = [ZeroCrossing]() let lastElementIndex = nextWriteIndex - 1 for i in 0...size - 1 { // Was Crashing here, now wrapped in lock / unlock lock.lock() let thisCrossing = array[(lastElementIndex - i) % size] lock.unlock() if thisCrossing.indexWithOffset > refIndex { subArray.append(thisCrossing) } else { break } } return subArray.reversed() } public func reset() { array = [ZeroCrossing](repeating: ZeroCrossing(index: 0, previousPeak: 0, indexWithOffset: 0), count: size) nextWriteIndex = 0 } }
Topic: Programming Languages SubTopic: Swift Tags:
Jun ’22
Reply to Bad access with a fixed size array and index in range
The i==0 occurs twice because there were no updates for the first one i.e. it read the element then if thisCrossing.indexWithOffset > refIndex returned false. I think you may be on to something with the thread comment, feedback from my stack exchange post suggested a thread problem too as installTap may run on a separate thread. I've just tried using an NSLock around each of the writes and reads in CrossingBuffer and it seems to prevent the crashes. I'll test a bit more to see if it seems like a permanent fix.
Topic: Programming Languages SubTopic: Swift Tags:
Jun ’22
Reply to Bad access with a fixed size array and index in range
I've added that print, here are the last lines of output before the crash occurred along with the backtrace. This was also with array.reserveCapacity(size) commented out. Also the array size is set to 639 for this example. i 0 lastElementIndex 13626 size 639 index 207 i 0 lastElementIndex 13627 size 639 index 208 i 1 lastElementIndex 13627 size 639 index 207 (lldb) bt * thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0x104690008)   frame #0: 0x000000018bf5c090 libswiftCore.dylib`swift_retain + 60   frame #1: 0x000000018bf9c704 libswiftCore.dylib`swift_bridgeObjectRetain + 56  * frame #2: 0x0000000102671200 RingBufferCrashing`CrossingBuffer.getAfterIndex(refIndex=739167.76999999583, self=0x0000000280b622e0) at CrossingBuffer.swift:55:31   frame #3: 0x000000010266ffb4 RingBufferCrashing`AudioTap.getNext(self=0x0000000282108180) at AudioTap.swift:81:33   frame #4: 0x00000001026701d0 RingBufferCrashing`@objc AudioTap.getNext() at <compiler-generated>:0 Edit: I just added a bit more of the backtrace as I'm wondering whether the compiler-generated:0 is a clue.
Topic: Programming Languages SubTopic: Swift Tags:
Jun ’22