Re: Saving NSDictionary, Enumeration error
Re: Saving NSDictionary, Enumeration error
- Subject: Re: Saving NSDictionary, Enumeration error
- From: Tamas Nagy <email@hidden>
- Date: Tue, 07 May 2013 23:32:00 +0200
Okay, maybe I misunderstood something, but since I collect the data to an NSMutableDictionary, then turn it to a non-mutable NSDictionary, like this:
-(NSDictionary)saveData {
NSMutableDictionary *dict = [[[NSMutableDictionary alloc] init] autorelease]
[dict setObject:...]
return NSDictionary dictionaryWithDictionary: [[dict copy] autorelease]];
so does not this made the dictionary already read-only?
On May 7, 2013, at 8:06 PM, Jens Alfke <email@hidden> wrote:
>
> On May 7, 2013, at 7:04 AM, Tamas Nagy <email@hidden> wrote:
>
>> The data collecting and save happening on the main thread, but the objects providing the data may run on an other - rendering - thread. Maybe its a problem with an object while collecting the save data, and I forgot about copy the value?
>
> Copying the value isn't enough! Foundation collections are not thread-safe — you shouldn't access a dictionary (or array or set or string) on one thread while modifying it on another. The only safe way to share these objects is read-only. (There's a whole document in Apple's doc-set about the thread-safety of various Cocoa classes. In general, never assume a class is thread-safe unless the docs explicitly say so.)
>
> In this specific crash, the presence of "__NSFastEnumerationMutationHandler" in the exception backtrace shows that the enumerator detected that the dictionary had been modified during the enumeration, which is illegal (even on a single thread.)
>
> —Jens
_______________________________________________
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