Re: crash on altering NSMutableArray
Re: crash on altering NSMutableArray
- Subject: Re: crash on altering NSMutableArray
- From: Michael Ash <email@hidden>
- Date: Sun, 2 May 2010 01:55:47 -0400
On Sun, May 2, 2010 at 1:50 AM, James Maxwell
<email@hidden> wrote:
>> On May 1, 2010, at 21:22, James Maxwell wrote:
>>
>>> If I drop into the debugger arbitrarily, before the crash, and check this same array, I noticed that it is nicely filled with NSCFNumbers. But, strangely, there are too many.
>>
>> My guess is that you've still got multiple threads accessing the mutable array simultaneously, and that's not thread-safe if one of the accesses is changing the array.
>
> Well, there's only one object that "changes" the array, so I don't know how this could happen.
It's a common misconception that the only worry with thread safety is
avoiding multiple writers.
All it takes to crash is a single write, happening simultaneous with a
read. A class that's not thread safe will not tolerate this, and can
crash. In general, multiple readers is fine, but the moment you make
any changes, you MUST not have ANY reads occurring for the duration of
the write.
Basically, unless you know enough about multithreading to start doing
really advanced things (and when I say "really advanced", I mean the
sort of level which most people never even reach), you MUST lock (or
otherwise protect, e.g. funneling through a single thread, GCD serial
queue, etc.) ALL accesses to any shared objects.
Mike
_______________________________________________
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