Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EXC_BAD_ACCESS during NSFileVersion call to removeOtherVersionsOfItemAtURL: inside coordinated write block

I'm using what seems to be a simple invocation of the NSFileVersion class method removeOtherVersionsOfItemAtURL: inside a coordinated writing block for some iCloud conflict resolution.

When my devices go into 'spaz mode', which is a technical term for repeatedly opening and closing the application on a few devices, an EXC_BAD_ACCESS exception is thrown internally. Code snippet:

- (void)compareVersionChanges:(NSFileVersion *)version {
    if (![DataLoader iCloudPreferenceEnabled]) {
        NSLog(@"Ignoring iCloud changes (version comparison) based on user preference");
        return;
    }
    NSLog(@"compareVersionChanges");
    dispatch_queue_t aQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_async(aQueue, ^(void) {
        NSError *readError = nil;
        NSFileCoordinator *coordinator = [[NSFileCoordinator alloc] initWithFilePresenter:(id)self];
        [coordinator coordinateReadingItemAtURL:[version URL] options:0 error:&readError byAccessor:^(NSURL *newURL) {
            DataContext *loadedContext = nil;
            NSData *data = [NSData dataWithContentsOfURL:newURL];
            NSError *e = nil;
            loadedContext = [self convertXmlDataToContext:data error:&e];
            if (e) {
                NSLog(@"Done loading, error: %@", e);
                [[DataLoader applicationDelegate] displayError:e];
                loadedContext = nil;
            }

            if (!loadedContext) {
                return;
            }

            id appDelegate = [DataLoader applicationDelegate];
            DataContext *inMemoryContext = nil;
            if (appDelegate != nil && [appDelegate respondsToSelector:@selector(context)]) {
                inMemoryContext = [appDelegate performSelector:@selector(context)];
            }

            if (inMemoryContext) {
                NSLog(@"Performing iCloud context synchronizating...");
                DataContextSynchronizer *synchronizer = [[DataContextSynchronizer alloc] init];
                ChangeSet *changes = [synchronizer compareLocalContext:inMemoryContext andRemoteContext:loadedContext];
                if ([[changes changes] count] > 0) {
                    [SelectionManager disable];
                    @synchronized(appDelegate) {
                        NSLog(@"Applying synchronization changes...");
                        [synchronizer applyChangeSet:changes toDataContext:inMemoryContext];
                        NSLog(@"Synchronization changes applied");
                    }
                    [SelectionManager enable];
                    if ([appDelegate respondsToSelector:@selector(setSkipRefreshSave:)]) {
                        [appDelegate performSelector:@selector(setSkipRefreshSave:) withObject:[NSNumber numberWithBool:YES]];
                    }
                    dispatch_queue_t mainQueue = dispatch_get_main_queue();
                    dispatch_async(mainQueue, ^(void) {
                        [SelectionManager notifyListeners];
                    });
                    if ([appDelegate respondsToSelector:@selector(setSkipRefreshSave:)]) {
                        [appDelegate performSelector:@selector(setSkipRefreshSave:) withObject:[NSNumber numberWithBool:NO]];
                    }
                    [self save:[[DataLoader applicationDelegate] context]];
                } else {
                    NSLog(@"No sync changes applicable.");
                }
                NSError *coordinateWriteRemoveError = nil;
                [coordinator coordinateWritingItemAtURL:newURL options:NSFileCoordinatorWritingForDeleting error:&coordinateWriteRemoveError byAccessor:^(NSURL *theURL) {
                    theURL = [theURL copy];
                    NSError *removeOtherVersionsError = nil;
                    [NSFileVersion removeOtherVersionsOfItemAtURL:theURL error:&removeOtherVersionsError];
                    if (removeOtherVersionsError) {
                        NSLog(@"Error removing other versions: %@", removeOtherVersionsError);
                    }
                }];
                if (coordinateWriteRemoveError) {
                    NSLog(@"Error occurred coordinating write for deletion of other file versions: %@", coordinateWriteRemoveError);
                }
            }
        }];


        if (readError) {
            NSLog(@"Done loading (outside block) error: %@", readError);
        }
    });
}

I thought a little syntax highlighting might make this easier to examine:

Link to image of code snippet and failure stack in Xcode

The error actually occurs on line 1404, and as you can see from the below screenshot, it's deep in Apple code territory.

Link to image of debugger

Before submitting a radar, I thought I'd check here to see if there's something I'm doing wrong? The extra [... copy] on line 1402 was just a quick check to make sure I'm not losing the reference to the block-provided argument, and will be removed.

Edit: An important note! I'm using ARC.

Edit 2: I've noticed that when calling:

[NSFileVersion otherVersionsOfItemAtURL:theURL]

The return value is nil, which indicates (via the documentation):

...or nil if there is no such file. The array does not contain the version object returned by the currentVersionOfItemAtURL: method.

So by checking the return value of this method before I make the call to removeOtherVersionsOfItemAtURL:, it has alleviated the issue. But I still find it strange that an EXC_BAD_ACCESS is thrown, rather than that method handling it properly.

like image 963
Craig Otis Avatar asked Nov 03 '22 16:11

Craig Otis


1 Answers

I've noticed that when calling:

[NSFileVersion otherVersionsOfItemAtURL:theURL]

immediately prior to the call to removeOtherVersionsOfItemAtURL:, the return value is nil, which indicates (via the documentation):

Returns: An array of file version objects or nil if there is no such file. The array does not contain the version object returned by the currentVersionOfItemAtURL: method.

So by checking the return value of this method before I make the call to removeOtherVersionsOfItemAtURL:, it has alleviated the issue. But I still find it strange that an EXC_BAD_ACCESS is thrown by removeOtherVersionsOfItemAtURL:, rather than that method simply returning NO, or simply populating the provided NSError object.

I'll be filing a Radar and will update here when I hear back.

like image 96
Craig Otis Avatar answered Nov 08 '22 05:11

Craig Otis