1

Currently, I see a recurring error:

2013-04-18 02:35:51.583 aaah[1713:1110f] *** Terminating app due to uncaught exception 'NSGenericException', reason: '*** Collection <__NSArrayM: 0x1fc00dc0> was mutated while being enumerated.'

In the failing background queue (I see my queues state upon the moment of a crash by pressing Command + 5) I see:

0 _pthread_kill
1 pthread_kill

And I see assembly output which I completely don't understand.

I know how to resolve this kind of array enumeration errors - I just can't understand where/how I should seek for the piece of code causing my app to crash.

I have much multi-threaded code using AFNetworking and Core Data, so I can't just remember where the crucial section might be.

Also, this error occurs not always, but from time to time, so it is difficult just to use "isolation approach", isolating smaller and smaller pieces of code, to finally identify the buggy piece of code.

So the question is:

How can I extract some more useful information from this output, or Xcode can provide me with some more verbosity level, so I could know how to resolve this annoying piece of code.

Stanislav Pankevich
  • 11,044
  • 8
  • 69
  • 129
  • Did you ever get an answer to this? – Shinigami Aug 02 '13 at 02:55
  • I have forgotten the original problem context, but generally setting exception breakpoints like it is described in the accepted answer allows me to understand what is going on when my apps crash. – Stanislav Pankevich Aug 02 '13 at 03:49
  • Haha... unfortunately not. I always have break on exception set. I have the exact problem you described here and the exception gets triggered in some nondescript assembly code or just comes out of nowhere from a random thread. It has been impossible to track down which part of my code causes it. – Shinigami Aug 02 '13 at 04:29

1 Answers1

2

Have you tried setting an exception breakpoint? Sometimes the debugger struggles with exception handling inside blocks but it might help.

In the breakpoints navigator (one of the tabs on the left side of the window) click the plus button at the bottom of the window and select add an exception breakpoint. Then hit done.

Now when you crash, the debugger should hopefully put you right before the instant the exception was raised.

See http://developer.apple.com/library/mac/#recipes/xcode_help-breakpoint_navigator/articles/adding_an_exception_breakpoint.html for more info.

Edit:

Based on the feedback provided in the comments, here's some more analysis:

The exception your application is raising means that you attempted to change a collection type (in this case, an NSMutableOrderedSet) inside of an enumeration block (in your case, most likely a for-loop). It looks like you're trying to remove objects in a set that you are looping over: [NSMutableOrderedSet removeObjectsInRange:inOrderedSet:]

To resolve this you should store the ranges you wish to remove and then do the actual removing once you're no longer iterating over the set. It's still going to be difficult since, as I understand it, you're not breaking inside of Objective-C code. You should look at the running threads in the panel on the left and look at what method they're currently in. That might give you a hint as to where your code is wrong.

Jack Lawrence
  • 10,664
  • 1
  • 47
  • 61
  • Thanks for the answer, indeed it is stopping before the instant the exception raises, and some other information appear in assembly: 0 objc_exception_throw 1 __NSFastEnumerationMutationHandler. And among the assembly I see strings like 0x32244d1a: blx 0x32283e7c ; symbol stub for: -[NSMutableOrderedSet removeObjectsInRange:inOrderedSet:]. But it all still does not help to understand the origin of my error. – Stanislav Pankevich Apr 18 '13 at 00:45
  • I understand you points, but that's what I am surprised by: I don't have any NSMutableOrderedSet classes/subclasses. – Stanislav Pankevich Apr 18 '13 at 00:55
  • Anyway, thank you for the hint about setting exception breakpoints - it is much better than having just default almost-nothing output. I will accept your answer and will continue my debugging routine. – Stanislav Pankevich Apr 18 '13 at 01:01
  • `NSMutableOrderedSet` Is related to Core Data. Are you using any ordered relationships? – Jack Lawrence Apr 18 '13 at 01:23
  • No, I am not using any! ordered relationships. – Stanislav Pankevich Apr 18 '13 at 01:30