index beyond bounds when using a queue for a for-loop
index beyond bounds when using a queue for a for-loop
- Subject: index beyond bounds when using a queue for a for-loop
- From: Koen van der Drift <email@hidden>
- Date: Mon, 12 Aug 2013 13:36:03 -0400
Hi,
I'm trying to use a queue for a calculation intensive for-loop to make my iOS app more responsive. The original for-loop works without errors:
for (NSUInteger i = 0; i < count; i++)
{
myArray[i] = [self doCalculation: i];
}
When count gets too large, there is a delay when the results are shown on the screen. So after reading through Apple's docs on concurrency, I implemented the following replacement:
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_apply(count, queue, ^(size_t i)
{
myArray[i] = [self doCalculation: i];
});
But after a few iterations, my app crashes because of:
*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM setObject:atIndex:]: index 5 beyond bounds [0 .. 1]'
*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM setObject:atIndex:]: index 3 beyond bounds [0 .. 0]'
*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM setObject:atIndex:]: index 2 beyond bounds for empty array'
Any thoughts why this happens, and how to fix it?
Thanks,
- Koen.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden