NSGenericException', reason: '*** Collection <__NSArrayM: 0x12a9f7d0> was mutated while being enumerated in MAPS

Durga Vundavalli picture Durga Vundavalli · Sep 30, 2013 · Viewed 7.5k times · Source

My stacktrace is related to maps..

NSGenericException occurs, if we try modify the array, which is being enumerated... I have taken-care about not modifying the enumerating Array.

for (int k=0;k<[[af factsArray] count];k++)
        //here af is my object
    {
    Facts *f = [[af factsArray] objectAtIndex:k];

    //here i'm removing unecessary characters from my point String

    NSCharacterSet *doNotWant = [NSCharacterSet characterSetWithCharactersInString:@"POINT()"];

    NSString *pointsStr = [[[f.factPosition mutableCopy] componentsSeparatedByCharactersInSet:doNotWant] componentsJoinedByString:@""];

    NSArray *pieces = [[pointsStr componentsSeparatedByString:@" "] copy];
    CGPoint point = CGPointMake([[pieces objectAtIndex:2] floatValue], [[pieces objectAtIndex:1] floatValue]);
    NSValue *val = [NSValue valueWithCGPoint:point];

    [routePointsArray addObject:val];

}

NSInteger pointsCount = routePointsArray.count;

CLLocationCoordinate2D pointsToUse[pointsCount];

for(int i = 0; i < pointsCount; i++)
{
    CGPoint p = [[routePointsArray objectAtIndex:i] CGPointValue];
    pointsToUse[i] = CLLocationCoordinate2DMake(p.x,p.y);
}

MKPolyline *myPolyline = [MKPolyline polylineWithCoordinates:pointsToUse     count:pointsCount];
[routeOverlays addObject:myPolyline];

NSLog(@"routeOverlays count:%d",[routeOverlays count]);


//adding overlays

dispatch_async(dispatch_get_main_queue(), ^{
    [self.map addOverlays:routeOverlays];
});

Can anybody tell me the problem with the code because it crashes sometimes and sometimes doesn't. here is my stacktrace

 *** Terminating app due to uncaught exception 'NSGenericException', reason: '*** Collection <__NSArrayM: 0x12a9f7d0> was mutated while being enumerated.'
        *** First throw call stack:
        (
            0   CoreFoundation                      0x01a2f5e4 __exceptionPreprocess + 180
            1   libobjc.A.dylib                     0x017b28b6 objc_exception_throw + 44
            2   CoreFoundation                      0x01abf3b5 __NSFastEnumerationMutationHandler + 165
            3   VectorKit                           0x04029d7e -[VKMapModel layoutScene:withContext:] + 3854
            4   VectorKit                           0x0401fc8e -[VKModelObject layoutSceneIfNeeded:withContext:] + 110
            5   VectorKit                           0x04028bff -[VKMapModel layoutSceneIfNeeded:withContext:] + 143
            6   VectorKit                           0x0401fd35 -[VKModelObject layoutSceneIfNeeded:withContext:] + 277
            7   VectorKit                           0x0403f278 -[VKWorld layoutScene:withContext:] + 56
            8   VectorKit                           0x0402eac6 -[VKScreenCanvas drawWithTimestamp:] + 358
            9   VectorKit                           0x04019c15 -[VKMapCanvas drawWithTimestamp:] + 149
            10  VectorKit                           0x0402e4ee -[VKScreenCanvas onTimerFired:] + 142
            11  libobjc.A.dylib                     0x017c481f -[NSObject performSelector:withObject:] + 70
            12  VectorKit                           0x041adbdc -[VGLDisplayLink _displayLinkFired:] + 60
            13  QuartzCore                          0x045f3fca _ZN2CA7Display15DisplayLinkItem8dispatchEv + 48
            14  QuartzCore                          0x045f3e86 _ZN2CA7Display11DisplayLink14dispatch_itemsEyyy + 310
            15  QuartzCore                          0x045f43ab _ZN2CA7Display16TimerDisplayLink8callbackEP16__CFRunLoopTimerPv + 123
            16  CoreFoundation                      0x019edc46 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 22
            17  CoreFoundation                      0x019ed62d __CFRunLoopDoTimer + 1181
            18  CoreFoundation                      0x019d5698 __CFRunLoopRun + 1816
            19  CoreFoundation                      0x019d4b33 CFRunLoopRunSpecific + 467
            20  CoreFoundation                      0x019d494b CFRunLoopRunInMode + 123
            21  GraphicsServices                    0x02c469d7 GSEventRunModal + 192
            22  GraphicsServices                    0x02c467fe GSEventRun + 104
            23  UIKit                               0x0031894b UIApplicationMain + 1225
            24  SnapTraq                            0x0002029d main + 141
            25  libdyld.dylib                       0x022c1725 start + 0
        )
        libc++abi.dylib: terminating with uncaught exception of type NSException

Answer

Durga Vundavalli picture Durga Vundavalli · Oct 1, 2013

I would like to thank @micantox for his attempt to solve my problem...

Anyways, I have solved my own problem.

I was trying to execute the loop from different threads.. that caused the actual problem.

one thread is add the annotations using the NSArray and other thread is removing those NSArray objects. to avoid this problem i have used

@synchronized(self)
  {
       // your code goes here 
  }