2

I have been using the Zbar SDK (1.2) in my project.

This is the crash reported by many users from last 8-9 months:

Message = * Terminating app due to uncaught exception 'NSGenericException', reason: '* Collection was mutated while being enumerated.'

Code:

- (void) imagePickerController: (UIImagePickerController*) reader
 didFinishPickingMediaWithInfo: (NSDictionary*) info
{
    // ADD: get the decode results
    id<NSFastEnumeration> results =
    [info objectForKey: ZBarReaderControllerResults];
    ZBarSymbol *symbol = nil;
    NSLog(@"%@",results);

    for(symbol in results)
        // EXAMPLE: just grab the first barcode
        break;

What's the cause of the problem?

Massimiliano Kraus
  • 3,638
  • 5
  • 27
  • 47
iGW
  • 633
  • 5
  • 14

1 Answers1

1

I know it's kinda late now, but maybe this helps future readers.

My theory is that the library removes one or more sets on the results object while app is enumerating through them. I solved this problem with a category that makes ZBarSymbolSet conform to NSCopying. This allows me to copy the set and enumerate on the copy. Category:

@interface ZBarSymbolSet (AllowCopy) <NSCopying>

@end

@implementation ZBarSymbolSet (AllowCopy)

- (id)copyWithZone:(NSZone *)zone
{
    id copy = [[[self class] alloc] initWithSymbolSet:self.zbarSymbolSet];
    return copy;
}

@end

Code:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    id result = [info valueForKey:ZBarReaderControllerResults];

    if ([result isKindOfClass:[ZBarSymbol class]])
    {
        ZBarSymbol *symbol = (ZBarSymbol *)result;
        // DO SOMETHING WITH SYMBOL
    }
    else if ([result isKindOfClass:[ZBarSymbolSet class]])
    {
        ZBarSymbolSet *set = (ZBarSymbolSet *)result;

        ZBarSymbolSet *copySet = set.copy;
        for (ZBarSymbol *symbol in copySet)
        {
            // DO SOMETHING WITH SYMBOL
        }
    }
}
Fabio Berger
  • 1,921
  • 2
  • 24
  • 29