• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: index beyond bounds when using a queue for a for-loop
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: index beyond bounds when using a queue for a for-loop


  • Subject: Re: index beyond bounds when using a queue for a for-loop
  • From: Conrad Shultz <email@hidden>
  • Date: Mon, 12 Aug 2013 12:07:00 -0700

On Aug 12, 2013, at 11:23 AM, Koen van der Drift <email@hidden> wrote:

>
> On Aug 12, 2013, at 2:05 PM, Jens Alfke <email@hidden> wrote:
>
>> NSMutableArray isn’t thread-safe. You’ll need to synchronize/serialize the assignments somehow. You could do something like
>>
>> {
>> 	id value = [self doCalculation: i];
>> 	@synchronized(myArray) {
>> 		myArray[i] = value;
>> 	}
>> }
>>
>
> Unfortunately, I'm still getting the same error.

I suspect there’s an additional complication. In your single-threaded case you probably assumed that `i` increased uniformly and monotonically, so that myArray[i] = foo always appended objects to the array. In a multithreaded implementation this is no longer the case: there’s a race between different values of `i`, so it’s entirely possible that you will try to, say, set the object at index 3 before index 2. This will raise a range exception as you are seeing.

There are at least two possible fixes.

First, since you know the array size in advance, you could do something like pre-fill myArray with [NSNull null]. This would work around the range exception by having your code replace objects rather than append new objects.

Second, you could switch from an array to an order-agnostic collection such as a dictionary or set. NSPointerArray may also provide useful behavior.

Exactly which approach you take will depend on specifics about your application and the problem you are solving.

-Conrad
_______________________________________________

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


  • Follow-Ups:
    • Re: index beyond bounds when using a queue for a for-loop
      • From: Koen van der Drift <email@hidden>
References: 
 >index beyond bounds when using a queue for a for-loop (From: Koen van der Drift <email@hidden>)
 >Re: index beyond bounds when using a queue for a for-loop (From: Jens Alfke <email@hidden>)
 >Re: index beyond bounds when using a queue for a for-loop (From: Koen van der Drift <email@hidden>)

  • Prev by Date: Core Data returns empty set
  • Next by Date: Re: index beyond bounds when using a queue for a for-loop
  • Previous by thread: Re: index beyond bounds when using a queue for a for-loop
  • Next by thread: Re: index beyond bounds when using a queue for a for-loop
  • Index(es):
    • Date
    • Thread