I'm trying to copy an array inside a specific queue and sometimes my app crashes with exc_bad_access
.
- (NSArray *)safeCopyArrayInQueue:(dispatch_queue_t)queue andArray:(NSArray *)arrayToCopy {
__block NSArray *copy = nil;
dispatch_sync(queue, ^{
@try {
copy = [NSArray arrayWithArray:arrayToCopy]; // this line crashes
}
@catch (NSException *exception) {
NSLog(@"%@", exception.description);
copy = @[];
}
});
return copy;
}
I found the issue thanks to Rohan Bhale - the problem occurred because I passed the array as a parameter and it was deallocated before I used it in [NSArray arrayWithArray:] method.
To fix it I used the array as a class variable and access it only inside its queue.