Search code examples
iphoneobjective-cnsexception

iPhone UI addSubview causing concurrency exception


This is really odd...

I run my app, and while it is opening and the views are constructing I get:

Collection <CALayerArray: 0x124650> was mutated while being enumerated.

The code trace goes through the following:

main
UIApplicationMain
-[UIApplication _run]
CFRunLoopRunInMode
CFRunLoopRunSpecific
_UIApplicationHandleEvent
-[UIApplication sendEvent:]
-[UIApplication handleEvent:withNewEvent:]
-[UIApplication _runWithURL:sourceBundleID:]
-[UIApplication _performInitilizationWithURL:sourceBundleID:]
-[AppDelegate applicationDidFinishLaunching:]
+[Controller initializeController] //This is my own function
    [window addSubview: pauseMenuController.view] //This is the last point of my code it goes through
-[UIView(Hierarchy) addSubview:]
-[UIView(Internal) _addSubview:positioned:relativeTo:]
-[UIView(Hierarchy) _makeSubtreePerformSelector:withObject:]
-[UIView(Hierarchy) _makeSubtreePerformSelector:withObject:withObject:copySublayers:]
-[UIView(Hierarchy) _makeSubtreePerformSelector:withObject:withObject:copySublayers:]
_NSFastEnumerationMutationHandler
objc_exception_throw

I've run the game lots and lots and lots of times and I've never seen this, then suddenly it popped up. The weird thing is that I'm not creating any other threads (that I know of) until after this code all gets called. It'll be easier for me to debug this if someone can give me some explanation of what might be getting modified while it's being accessed in a UIView. Does it have something to do with adding something to the view while it's already adding something, maybe? Any ideas?


Solution

  • It is not a concurrency exception. Enumeration mutation exceptions happen when something (possibly inside a loop in the same thread) changes the array/set/dictionary that the loop is iterating.

    Since addSubview: is on the stack, my guess is that something is trying to remove one of the UIView's subviews during construction.

    Are you overriding any methods that may be getting run? Like addSubview or possibly layoutSubviews? If any of these is removing subviews, then this would cause the problem.