3

I have a pretty simple IOS app using iCloud document storage. Everything was working and then at some point I began encountering a EXC_BAD_ACCESS error in my document load method for at least one iCloud document, although most files load just fine.

- (BOOL)loadFromContents:(id)contents ofType:(NSString *)typeName error:(NSError *__autoreleasing *)outError {

    file = (NSFileWrapper*) contents;

    NSFileWrapper *infoFile = [[file fileWrappers] objectForKey:InfoFile];
    NSData *infoData = [infoFile regularFileContents];

    if(nil != infoData) {

        NSPropertyListFormat format = NSPropertyListBinaryFormat_v1_0;
        NSError *propertyListError;

        // EXC_BAD_ACCESS occurs here
        NSDictionary *dictionary = [NSPropertyListSerialization propertyListWithData:infoData options:NSPropertyListImmutable format:&format error:&propertyListError];

        if(nil == propertyListError) {

            _name = [dictionary objectForKey:@"name"];
            _date = [dictionary objectForKey:@"date"];
            _index = [dictionary objectForKey:@"index"];
            _paperSize = [GritzPaperSizeEnum enumWithType:[dictionary objectForKey:@"paperSize"]];

            TFLog(@"loading doc %@", _name);

            _pages = [[NSMutableArray alloc] init];

            for (NSString *key in file.fileWrappers) {

                NSFileWrapper *subDir = [[file fileWrappers] objectForKey:key];

                if(subDir.isDirectory) {
                    GritzPage *page = [[GritzPage alloc] initFromFile:subDir];
                    [_pages addObject:page];
                }
            }

            _currentPage = [_pages objectAtIndex:0];

            return YES;
        }
    }

    return NO;
}

enter image description here

I would expect that I can 'catch' and handle bad data and ignore the corrupt file; but I can't seem to figure out how. A EXC_BAD_ACCESS error causes the app to crash.

What should I be doing differently to determine ahead of time that the data or file is going to fail and skip it (or delete it).

Scott Boring
  • 2,601
  • 1
  • 25
  • 31
  • me too having the similar issue...can you show us how you fixed it? It would be very helpful for me. – Ananth Jan 22 '13 at 14:06

2 Answers2

1

verify it is a NSFileWrapper using isKindOfClass, else treating it as one is weird (also look at the given typeName :))


using a @try { .. } @catch construct to catch any exception wont work in THIS case though as you cause a BAD_ACCESS which is a UNIX SIGNAL

Daij-Djan
  • 49,552
  • 17
  • 113
  • 135
  • I tried checking the file types for 'content' and 'infoFile' and both passed as NSFileWrappers. I would expect this anyway since the failure occurs when trying to convert 'infoData' into an NSDictionary. I would suspect, that the 'infoData' files contains invalid XML and thus can't be deserialized. I would expect that I could identify this with exception/error handling code, but there is no way to handle a BAD_ACCESS error. – Scott Boring Dec 16 '12 at 19:00
0

The NSPropertyListFormat variable format should be declare as point. And i think you should call the propertyListWithData: Method with format as pointer not with the address of format.

Areal-17
  • 406
  • 3
  • 10