iOS内存警告案例崩溃并出现错误"message sent to deallocated instance"



将我的视图控制器两次推送到导航控制器并模拟内存警告,导致应用程序崩溃并出现错误:"message sent to deallocated instance"

我通过按下按钮来推动视图控制器:

-(void)buttonPressed
{
    MyViewCOntroller *vc = [[MyViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

    场景:

    1. [self buttonPressed];
    2. 在vc中按后退键
    3. [self buttonPressed];
    4. 在vc中按后退键
    5. 模拟内存警告

    如果只按一次,不会发生崩溃。

    我也试图移动"vc"的父控制器的ivar,但效果是相同的…

    也许这将帮助,但我使用自定义后退按钮和它的选择器:

    -(void)backButtonPressed
    {
        [self.navigationController popViewControllerAnimated:YES];
    }
    

    创建vc对象作为类成员并检查是否存在crash

     @interface ParentViewController()
     {
        MyViewCOntroller *vc;
     }
     @end
     @implementation PaintingViewController
    
      -(void)buttonPressed
      {
         vc = [[MyViewController alloc] init];
         [self.navigationController pushViewController:vc animated:YES];
      }
    

    找到解决方案了。请看我对另一个问题的回答:

    [UINavigationController retain]:消息发送到已分配的实例

    最新更新