I use Grand Central Dispatch to convert elements from one array to another. I call dispatch_applyin the source array, convert it to zero or more elements, and then add them to the target array. This is a simplified example:
let src = Array(0..<1000)
var dst = [UInt32]()
let queue = dispatch_queue_create("myqueue", DISPATCH_QUEUE_CONCURRENT)
dispatch_apply(src.count, queue) { i in
dst.append(arc4random_uniform(UInt32(i)))
}
print(dst)
I sometimes get an error in the line append. The error is always one of:
1. malloc: *** error for object 0x107508f00: pointer being freed was not allocated
2. fatal error: UnsafeMutablePointer.destroy with negative count
3. fatal error: Can't form Range with end < start
I assume this is due to the fact that appendit is not thread safe. What have I done wrong and how to fix it?
source
share