Search code examples
iosios5

Crash with "Collection ... mutated while being enumerated"


I got this error after a crash:

malloc: *** error for object 0x75617b4: incorrect checksum for freed object - object was probably modified after being freed.
*** set a breakpoint in malloc_error_break to debug
2013-02-05 19:15:44.950 BusinessIdea[3171:15e03] *** Terminating app due to uncaught exception 'NSGenericException', reason: '*** Collection <__NSSetM: 0x72dc5c0> was mutated while being enumerated.'

I am not familiar with this error. Any idea what it could be?


Solution

  • The whole 'modified while being enumerated' error means you are probably trying to delete something from a set while iterating over it. For example

    for(NSString *str in array){
         if([str isequalToString:@"delete me"]){
             [array removeObject:str];   //this will cause a problem,
         }
    }   
    

    If you want to use the fast enumeration loop, you need to build a list of items you want to remove, and then delete them after the iteration step. Alternatives are to use a traditional indexed for loop if you want to remove items in the loop body.