【问题标题】:How to debug: *** -[<func_name> controllerWillChangeContent:]: message sent to deallocated instance 0x5909c60如何调试:*** -[<func_name> controllerWillChangeContent:]:消息发送到已释放实例 0x5909c60
【发布时间】:2011-12-04 03:06:01
【问题描述】:

我是否错误地声明或使用了“课程”变量?我需要将用户选择的课程对象发送到子 UIViewController 并且没有任何运气。此代码运行 2 次,然后第三次失败。

我得到的运行时错误是:

2011-10-09 17:04:41.403 [] *** -[vcListGrades controllerWillChangeContent:]: message sent to deallocated instance 0x5909c60

当我使用调试器命令“info malloc-history 0x5909c60”找到该地址时,它会将我指向下面显示的代码。

// 这是有问题的代码

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

    [tableView deselectRowAtIndexPath:(NSIndexPath *)indexPath animated:YES];   

    course  = [_fetchedResultsController objectAtIndexPath:indexPath];  

    //create the new controller for next drill level into table
    vcListGrades *listGradesViewController = [[vcListGrades alloc] initWithNibName:@"vcListGrades" bundle:nil];    

// ^^^ 上面这一行是错误标记的行。

    // take the MO context with you to the next level of table drilling
    listGradesViewController.managedObjectContext = self.managedObjectContext;

    // take the school_courseName record that was just clicked  with you as you drill into next table
    [listGradesViewController setCourse: course];

    // deselect the row that was just clicked - this according to mac style guide
    [tableView deselectRowAtIndexPath:(NSIndexPath *)indexPath animated:YES];

    //push new controller onto stack and go for it
    [self.navigationController pushViewController:listGradesViewController animated:YES];

    [listGradesViewController release];

}

//这是.h文件中如何定义课程

@interface vcListCourses : UITableViewController <NSFetchedResultsControllerDelegate> {

    NSFetchedResultsController *_fetchedResultsController;
    NSManagedObjectContext *managedObjectContext;
    Schoolyear *_schoolyear;
    Course *course;

}

...elipse
@property (nonatomic, retain) Course *course;

//在.m文件中合成行

@synthesize course;

这里是 info malloc-history 堆栈

(gdb) info malloc-history 0x5909c60
Alloc: Block address: 0x05909c60 length: 176
Stack - pthread: 0xacff42c0 number of frames: 19
    0: 0x991e990b in malloc_zone_calloc
    1: 0x991ea837 in calloc
    2: 0x11322d4 in class_createInstance
    3: 0xefe5d8 in +[NSObject(NSObject) allocWithZone:]
    4: 0xefe3da in +[NSObject(NSObject) alloc]
    5: 0x83b6 in -[vcListCourses tableView:didSelectRowAtIndexPath:] at vcListCourses.m:428
    6: 0x36ab68 in -[UITableView _selectRowAtIndexPath:animated:scrollPosition:notifyDelegate:]
    7: 0x360b05 in -[UITableView _userSelectRowAtPendingSelectionIndexPath:]
    8: 0x7279e in __NSFireDelayedPerform
    9: 0xfbb8c3 in __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__
   10: 0xfbce74 in __CFRunLoopDoTimer
   11: 0xf192c9 in __CFRunLoopRun
   12: 0xf18840 in CFRunLoopRunSpecific
   13: 0xf18761 in CFRunLoopRunInMode
   14: 0x19311c4 in GSEventRunModal
   15: 0x1931289 in GSEventRun
   16: 0x301c93 in UIApplicationMain
   17: 0x26e9 in main at main.m:14
   18: 0x2665 in start

这看起来对吗?

我在 rootviewcontroller.h 文件中这样定义我的 nsfetchedResultsController

The @interface RootViewController : UITableViewController  <NSFetchedResultsControllerDelegate> {

    NSFetchedResultsController *_fetchedResultsController;
    NSManagedObjectContext *managedObjectContext;
    UIBarButtonItem *addButton;
    Schoolyear *year;
}

@property (nonatomic, retain) NSFetchedResultsController *fetchedResultsController;

这是我的 NSFetchedResultsController 的 .m 文件

@synthesize fetchedResultsController = _fetchedResultsController;

【问题讨论】:

  • 抓取结果控制器是如何创建的?
  • 什么是课程?你不需要做[listGradesViewController setCourse: [course retain]];
  • @ott - 我尝试了上面的代码,但没有任何乐趣。注意更新 - 违规行实际上是它下方的行,我在其中分配了新的视图控制器。
  • @PaulMason - Paul,请参阅上面对原始帖子的编辑以查看 nsfetchedresultscontroller 的定义。谢谢!
  • 我没有想法,请尝试在 tableView:didSelectRowAtIndexPath 的开头添加一个断点:按照简单的步骤操作代码,看看这是否会破坏您的代码。顺便说一句,你有一个双 [tableView deselectRowAtIndexPath:(NSIndexPath *)indexPath animated:YES];除非那是:P

标签: iphone memory-management dealloc


【解决方案1】:

请显示异常堆栈。当然,您会意识到,当您分配“课程”时,它不会被保留。要保留您需要分配给“self.course”。

【讨论】:

  • 我最初指出违规行 (428) 是“课程”分配行。有问题的线实际上是它下面的线。这里是:vcListGrades *listGradesViewController = [[vcListGrades alloc] initWithNibName:@"vcListGrades" bundle:nil]; //
  • 我没有问 malloc-history 标记了哪一行,我问的是异常堆栈。
  • 和 malloc-history 栈一样吗?我已经在上面的原始内容中添加了这一点。
  • 您遇到了运行时错误。除非你特别倒霉,否则紧接着就是异常堆栈。
  • 知道了,丹尼尔。感谢您的帮助。我未能在 dealloc() 例程中将获取的控制器设置为 nil,向下钻取几个视图。幸运地找到了它 - 只是在代码中磕磕绊绊并绊倒了它。都好。再次感谢。
猜你喜欢
  • 2023-03-13
  • 2011-03-29
  • 1970-01-01
  • 2011-06-16
相关资源
最近更新 更多