【问题标题】:Object dealloc only crashes in iOS 4.3对象 dealloc 仅在 iOS 4.3 中崩溃
【发布时间】:2011-12-26 20:13:30
【问题描述】:

我试图弄清楚为什么使用 didSelectRowAtIndexPath 在我的 tableView 中推送 viewController 会导致 iOS 4.3 崩溃,但在 iOS 5.0+ 中,它可以正常工作。

当我打电话时它就崩溃了:

self.customViewController = [[[CustomViewController alloc] initWithNibName:@"CustomViewController"bundle:nil] autorelease];

第一次推送 customViewController 之后的任何时间。

这是我的相关代码:

@property (nonatomic, retain) CustomViewController *customViewController;

-(void) dealloc // Dealloc of tableView.
{

[customViewController release];
customViewController = nil;

}


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

self.customViewController = [[[CustomViewController alloc] initWithNibName:@"CustomViewController"bundle:nil] autorelease]; // Release old, allocate new, set it.

[[self navigationController] pushViewController:customViewController animated:YES];
[customViewController release]; // Balance out pushViewController's retain.


}

谢谢。

【问题讨论】:

    标签: iphone ios uitableview crash pushviewcontroller


    【解决方案1】:
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
    self.customViewController = [[[CustomViewController alloc] initWithNibName:@"CustomViewController"bundle:nil] autorelease];
    [[self navigationController] pushViewController:customViewController animated:YES];
    [customViewController release]; // Balance out pushViewController's retain. ---->NO
    }
    

    最后一个release 是多余的,不需要。
    您已经在其上完成了 autorelease 以将其 retain count 减一。


    我们将分析这一行

    self.customViewController = [[[CustomViewController alloc] initWithNibName:@"CustomViewController"bundle:nil] autorelease];
    

    您创建了一个CustomViewController 保留计数 == 1。
    你在上面说autorelease,所以保留计数稍后会为0(可能是运行循环的结尾),但现在它仍然是1,这就是为什么你仍然可以访问它,但把它当作0。
    之后你说self.customViewController,那个属性是retain,所以retain count == 1。 你正在处理你的 dealloc 中的那个 1。
    根据您的评论:

    // 平衡pushViewController的retain。

    你不Balance那些,你只平衡YOU own。如果系统对您的对象进行保留,当系统不再需要它们时,它会释放它们。

    【讨论】:

    • 认真等待?我真的以为它保留了viewController,但我猜它只是保留了视图?
    • @JoeRaythein 它确实保留了视图控制器,但它也释放了它。它拥有它的所有权。当您调用 autorelease 时,您已经放弃了所有权。
    • 但是我认为release不应该在控制器被推送时使应用程序崩溃,它应该在dealloc方法上崩溃而不是didSelectRowAtIndexPath
    • 我的措辞很奇怪,但你是对的,当我们解除分配它时它会崩溃。它实际上有点像 NSLogs 一个释放消息的无限循环(我注意到当 customViewControllers 被释放以进行调试时),但与崩溃相同。
    • @JoeRaythein 只有当您选择另一行,或者当您的UITableViewController 子类的Dealloc(我认为)被释放时,您的代码才会被释放,因为它被保留了由您的财产。我希望它在pop 离开屏幕时被释放,不要存储在属性中。阅读我之前的评论。
    【解决方案2】:

    不要释放 customViewController。您在分配它时已经自动释放它,因此您已经放弃了alloc 的所有权。您不必再次释放或自动释放对象。导航控制器取得所有权,并将在适当的时候自行放弃。

    另外,巧合的是,您可能会在一个版本中看到它而不是另一个版本。这是一个内存管理问题,因此您可能看到的任何损坏(崩溃等)都将取决于您运行应用程序时设备上的内存状态。您可能会一直看到崩溃,永远不会看到崩溃,或者只是在运行 Skype 之后但在打开照片应用之前看到。

    寻找这些的一个好方法是在调试会话期间启用僵尸。启用僵尸后,对象永远不会真正释放。相反,它们会进入僵尸状态,如果它们再次发送消息,它们将中止应用程序并向您显示杂散消息的发送位置,以帮助您调试内存问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-14
      • 2012-07-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多