EXC_BAD_ACCESS - NSFetchedResultsController, UITableViewController, UINavigationController, UIPopoverController

StackOverflow https://stackoverflow.com/questions/8819591

سؤال

I have a UITableViewController inside a UINavigationController, inside a UIPopoverController.

The UITableViewController uses a NSFetchedResultsController. didSelectRowAtIndexPath pushes another instance of my UITableViewController with a slightly different predicate onto the nav controller stack.

If I push a new UITableViewController on to the stack, and then pop it again, I will eventually get a EXC_BAD_ACCESS if I try to save an object that would have updated the tableview that was popped off.

As expected, setting the delegate of my NSFetchedResultsController to nil removes the EXC_BAD_ACCESS errors.

I am using ARC. So clearly these objects are getting released. This is OK. But why are they still getting notified when there is a change?

Code below. I am basically tracking the history of a web view in my database.

BookmarkViewController * bookmarkController = [[BookmarkViewController alloc] initWithStyle:UITableViewStylePlain andWebView:self.webView];
UINavigationController * bookmarkNavController = [[UINavigationController alloc] initWithRootViewController:bookmarkController];
self.bookmarkPopover = [[UIPopoverController alloc] initWithContentViewController:bookmarkNavController];
_bookmarkPopover.popoverContentSize = CGSizeMake(320, 44*10);
_bookmarkPopover.delegate = self;
bookmarkController.container=_bookmarkPopover;
bookmarkController.delegate=self;

The BookmarkViewController uses an NSFetchedResultsController and BookmarkViewController is the delegate of the NSFetchedResultsController.

- (NSFetchedResultsController *) myFetchedResultsController
{   
    if (self.fetchedResultsController != nil) {
        return self.fetchedResultsController;
    }   
    // Singleton
    CoreDataManager * dataManager = [CoreDataManager defaultDataManager];

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Bookmark" inManagedObjectContext:dataManager.managedObjectContext];
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"label" ascending:NO];
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];

    [fetchRequest setEntity:entity];
    [fetchRequest setPredicate:self.predicate];
    [fetchRequest setSortDescriptors:sortDescriptors]; 
    [fetchRequest setFetchBatchSize:20]; // Set the batch size to a suitable number.

    self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
                                                                    managedObjectContext:dataManager.managedObjectContext
                                                                      sectionNameKeyPath:@"type"
                                                                               cacheName:nil];
    self.fetchedResultsController.delegate = self;

    return self.fetchedResultsController;
}

And also I have:

- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller {    
    [self.tableView beginUpdates];
}

- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
    [self.tableView endUpdates];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    BookmarkViewController * bookmarkViewController = [[BookmarkViewController alloc] initWithStyle:self.tableView.style 
                                                                                         andWebView:self.webview 
                                                                                          forFolder:bookmark.label];
    [self.navigationController pushViewController:bookmarkViewController animated:YES];
}
هل كانت مفيدة؟

المحلول

I found some related questions:

Deallocated view controller causing EXC_BAD_ACCESS because of fetched results controller update

If I release, I get bad access, if I retain, I leak

The 2nd link gave me the idea of setting the delegate to nil in the dealloc method

-(void) dealloc
{
    self.fetchedResultsController.delegate = nil;
}

But I am using ARC so I cannot call [super dealloc] explicitly. Appears to fix the problem, but I am not sure this is correct. Should I also set the rest of my local variables to nil? If this overrides what ever the compiler generates, will it leak?

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top