代码之家  ›  专栏  ›  技术社区  ›  mootymoots

核心数据删除对象崩溃应用程序,具有EXC_BAD_访问权限

  •  1
  • mootymoots  · 技术社区  · 14 年前

    -(void)loadMainUser {
    
    id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:0];
    
    if([sectionInfo numberOfObjects]) {
    
        NSUInteger indexArr[] = {0,0};
        NSIndexPath *indexSet = [NSIndexPath indexPathWithIndexes:indexArr length:2];
        mainUser = (User *)[self.fetchedResultsController objectAtIndexPath:indexSet];
    
        NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"siteName" ascending:YES selector:@selector(caseInsensitiveCompare:)];
        NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:&sortDescriptor count:1];
    
        sortedMsgs = [[mainUser.sitesToUser allObjects] mutableCopy];
        [sortedMsgs sortUsingDescriptors:sortDescriptors];
    
        [sortDescriptor release];
        [sortDescriptors release];      
    }
    
    [self.theTableView reloadData];
    
    }
    

    现在我的方案是删除这个用户对象和相关站点。但是,每当我尝试保存删除时,我的应用程序就会因EXC_BAD_ACCESS错误而崩溃。这是我的删除代码:

    -(void)deleteUser {
    
    sortedMsgs = nil;
    mainUser = nil;
    
    NSUInteger indexArr[] = {0,0};
    NSIndexPath *indexSet = [NSIndexPath indexPathWithIndexes:indexArr length:2];
    
    NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
    
    [context deleteObject:[self.fetchedResultsController objectAtIndexPath:indexSet]];
    // Save the context.
    
    NSError *error = nil;
    if (![context save:&error]) {       
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    } else {        
        // do something
    }
    }
    

    我不完全确定为什么。。。我注意到我的主用户的保留计数是3,我认为这是因为sortedMsgs和UITableView。。。是因为我的UITableView仍然有效地显示来自用户的数据,然后我删除它吗?真的很困惑。。。

    Exception Type:  EXC_BAD_ACCESS (SIGBUS)
    Exception Codes: KERN_PROTECTION_FAILURE at 0x0000000000000016
    Crashed Thread:  0  Dispatch queue: com.apple.main-thread
    
    Application Specific Information:
    objc_msgSend() selector name: isTemporaryID
    iPhone Simulator 235, iPhone OS 4.2 (iPhone/8C134)
    
    Thread 0 Crashed:  Dispatch queue: com.apple.main-thread
    0   libobjc.A.dylib                 0x01613a63 objc_msgSend + 23
    1   CoreData                        0x00e20d26 -[NSManagedObjectContext save:] + 566
    2   Clicky                          0x00002cfd -[RootViewController deleteUser] + 358 (RootViewController.m:71)
    3   UIKit                           0x003eaa6e -[UIApplication sendAction:to:from:forEvent:] + 119
    4   UIKit                           0x005f8167 -[UIBarButtonItem(UIInternal) _sendAction:withEvent:] + 156
    5   UIKit                           0x003eaa6e -[UIApplication sendAction:to:from:forEvent:] + 119
    6   UIKit                           0x004791b5 -[UIControl sendAction:to:forEvent:] + 67
    
    1 回复  |  直到 10 年前
        1
  •  1
  •   Verbose    14 年前

    我现在唯一的猜测是,您可能正在删除一个与另一个对象有必需关系的对象。检查数据模型中的关系?

    完全是预感。