这个 NSZombie 错误消息是啥意思?

Posted

技术标签:

【中文标题】这个 NSZombie 错误消息是啥意思?【英文标题】:What Does This NSZombie Error Message Mean?这个 NSZombie 错误消息是什么意思? 【发布时间】:2011-04-29 05:19:26 【问题描述】:

我打开僵尸是因为我遇到了一些崩溃。现在我在控制台中收到此错误。有谁知道是什么意思吗?

*** -[RoutineDayTableViewController retain]: message sent to deallocated instance 0x7464150

@implementation RoutineDayTableViewController

@synthesize fetchedResultsController;
@synthesize exerciseChooserView;
@synthesize routineTableView;
@synthesize managedObjectContext;
@synthesize selectedExercise;
@synthesize theSelectedRoutine;

- (void)dealloc

    NSLog(@"dealloc");
    [fetchedResultsController release];
    [selectedExercise release];
    [managedObjectContext release];
    [exerciseChooserView release];
    [routineTableView release];
    [theSelectedRoutine release];
    [super dealloc];


- (void)didReceiveMemoryWarning

    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];


#pragma mark - View lifecycle

- (void)viewDidLoad

    [super viewDidLoad];

    self.routineTableView.delegate = self;
    if (managedObjectContext == nil) 
     
        managedObjectContext = [(CurlAppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext]; 
        [managedObjectContext retain];
    


- (void)viewDidUnload

    NSLog(@"viewDidUnload");
    [super viewDidUnload];
    self.exerciseChooserView = nil;
    self.routineTableView = nil;
    self.fetchedResultsController = nil;


#pragma mark - Exercise Editing

-(IBAction)exerciseChooser

    RoutineExerciseChooserViewController *routineExerciseChooserViewController = [[[RoutineExerciseChooserViewController alloc] init] autorelease];
    [self.navigationController pushViewController:routineExerciseChooserViewController animated:YES];


-(void)addExercise
   
    if (managedObjectContext == nil) 
     
        managedObjectContext = [(CurlAppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext]; 
        [managedObjectContext retain];
    

    UIBarButtonItem *addButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(exerciseChooser)];
    self.navigationItem.rightBarButtonItem = addButton;
    [addButton release];

    NSError *error = nil;

    Exercise *exercise = (Exercise *)[NSEntityDescription insertNewObjectForEntityForName:@"Exercise" inManagedObjectContext:managedObjectContext];

    exercise.name = self.selectedExercise;

    NSLog(@"addExercise theSelectedRoutine:  %@", theSelectedRoutine);

    [self.theSelectedRoutine addRoutineToExercisesObject:exercise];

    if (![fetchedResultsController.managedObjectContext save:&error]) 
    
        // Handle the error.
    
    NSLog(@"%@", error);
    NSLog(@"addExercise theSelectedRoutine:  %@", theSelectedRoutine);




#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

    return [[self.fetchedResultsController sections] count];


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

    id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
    return [sectionInfo numberOfObjects];


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [routineTableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) 
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    

    Exercise *tempExercise = (Exercise *)[fetchedResultsController objectAtIndexPath:indexPath];
    cell.textLabel.text = tempExercise.name;

    return cell;


-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath

    if (editingStyle == UITableViewCellEditingStyleDelete) 
    
        // Delete the managed object for the given index path
        NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
        [context deleteObject:[self.fetchedResultsController objectAtIndexPath:indexPath]];
        NSLog(@"fetched results : \n%@\n",[self.fetchedResultsController fetchedObjects]);

        // Commit the change.
        NSError *error = nil;

        // Update the array and table view.
        if (![fetchedResultsController.managedObjectContext save:&error]) 
        
            // Handle the error.
        
        //[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
    


- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath

    NSManagedObject *managedObject = [self.fetchedResultsController objectAtIndexPath:indexPath];
    cell.textLabel.text = [[managedObject valueForKey:@"name"] description];


#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

    // Navigation logic may go here. Create and push another view controller.
    [routineTableView deselectRowAtIndexPath:indexPath animated:YES];
     DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];
     // Pass the selected object to the new view controller.
     [self.navigationController pushViewController:detailViewController animated:YES];
     [detailViewController release];


#pragma mark - Fetched results controller

- (NSFetchedResultsController *)fetchedResultsController

    if (fetchedResultsController != nil)
    
        return fetchedResultsController;
    

    // Create the fetch request for the entity.
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    // Edit the entity name as appropriate.
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Exercise" inManagedObjectContext:self.managedObjectContext];
    [fetchRequest setEntity:entity];
    NSLog(@"fetchedResultsController theSelectedRoutine: %@",theSelectedRoutine);
    [fetchRequest setPredicate:[NSPredicate predicateWithFormat: @"ANY exerciseToRoutine == %@", theSelectedRoutine]];

    // Set the batch size to a suitable number.
    [fetchRequest setFetchBatchSize:20];

    // Edit the sort key as appropriate.
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:NO];
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];

    [fetchRequest setSortDescriptors:sortDescriptors];

    // Edit the section name key path and cache name if appropriate.
    // nil for section name key path means "no sections".
    NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:nil];
    aFetchedResultsController.delegate = self;
    self.fetchedResultsController = aFetchedResultsController;

    NSError *error = nil;
    if (![self.fetchedResultsController performFetch:&error])
    
        /*
         Replace this implementation with code to handle the error appropriately.

         abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button.
         */
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    

    [aFetchedResultsController release];
    [fetchRequest release];
    [sortDescriptor release];
    [sortDescriptors release];
    return fetchedResultsController;
    

#pragma mark - Fetched results controller delegate

- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller

    [self.routineTableView beginUpdates];


- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo
           atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type

    switch(type)
    
        case NSFetchedResultsChangeInsert:
            [self.routineTableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
            break;

        case NSFetchedResultsChangeDelete:
            [self.routineTableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
            break;
    


- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject
       atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type
      newIndexPath:(NSIndexPath *)newIndexPath

    UITableView *tableView = self.routineTableView;

    switch(type)
    

        case NSFetchedResultsChangeInsert:
            [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
            break;

        case NSFetchedResultsChangeDelete:
            [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
            break;

        case NSFetchedResultsChangeUpdate:
            [self configureCell:[tableView cellForRowAtIndexPath:indexPath] atIndexPath:indexPath];
            break;

        case NSFetchedResultsChangeMove:
            [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
            [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]withRowAnimation:UITableViewRowAnimationFade];
            break;
    


- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller

    [self.routineTableView endUpdates];


@end

更新,这个方法在另一个viewController中。这会导致问题吗?

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    
        [tableView deselectRowAtIndexPath:indexPath animated:YES];

        NSString *selectedRow = [[self.exerciseArray objectAtIndex:indexPath.row]objectForKey:@"exerciseName"];
        NSLog(@"Row Selected: %@", selectedRow);

        RoutineDayTableViewController *routineDayTableViewController=[self.navigationController.viewControllers objectAtIndex:([self.navigationController.viewControllers count] -3)];

        routineDayTableViewController.selectedExercise = selectedRow;
        [routineDayTableViewController addExercise];
        [routineDayTableViewController release];

        [self dismissView];
  

这是访问此类的另一个 viewController 中的另一个方法:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

    [tableView deselectRowAtIndexPath:indexPath animated:YES];

     RoutineDayTableViewController *detailViewController = [[RoutineDayTableViewController alloc] initWithNibName:@"RoutineDayTableViewController" bundle:nil];
    NSManagedObject *managedObject = [self.fetchedResultsController objectAtIndexPath:indexPath];

    detailViewController.title = [[managedObject valueForKey:@"name"] description];

    detailViewController.theSelectedRoutine = [__fetchedResultsController objectAtIndexPath: indexPath];
    NSLog(@"detailViewController.theSelectedRoutine:%@",detailViewController.theSelectedRoutine);
    [self.navigationController pushViewController:detailViewController animated:YES];
    [detailViewController release];

【问题讨论】:

【参考方案1】:

错误消息意味着您正在向保留计数为零并随后被释放的对象发送消息。根据您的代码 sn-p,我认为您的问题是这段代码-

    RoutineDayTableViewController *routineDayTableViewController=[self.navigationController.viewControllers objectAtIndex:([self.navigationController.viewControllers count] -3)];

    routineDayTableViewController.selectedExercise = selectedRow;
    [routineDayTableViewController addExercise];
    [routineDayTableViewController release];

这里的release 电话看起来很可疑。你正在释放一些你没有在这里分配的东西。

您是否在代码上运行过静态分析器?它有助于检测此类错误。

【讨论】:

我认为这实际上已经解决了问题。谢谢!【参考方案2】:

RoutineDayTableViewController 类的实例是问题所在,因此您在此处发布的代码中不太可能出现此问题。

检查你使用这个类的代码。

它可以帮助您使用分析选项进行构建,这通常会检测到问题。

【讨论】:

谢谢,我添加了另一个可能导致问题的类的方法?【参考方案3】:

在某些地方,RoutineDayTableViewController 类的实例被释放,之后您尝试访问它。

如果要检查,请不要释放该对象并尝试运行。

【讨论】:

谢谢,我添加了另一个可能导致问题的类的方法?

以上是关于这个 NSZombie 错误消息是啥意思?的主要内容,如果未能解决你的问题,请参考以下文章

发邮件时提示这个错误是啥意思,邮件也收不到别人邮件了

在 DataGrip 错误消息中,“位置”是啥意思?

当我尝试在 R 中安装 Mosaic 包时,这个错误是啥意思?

这个张量流消息是啥意思?

使用 GCM 发送推送消息时,错误代码“notRegistered”是啥意思?

H2 数据库 - 错误消息中的 SELECTIVITY 是啥意思