【问题标题】:ARC releasing ViewController prematurelyARC 过早释放 ViewController
【发布时间】:2012-03-12 12:26:23
【问题描述】:

我是 ARC 的新手,我玩它还不到一周。我想做的是非常基本的。我有一个显示按钮的视图控制器。当我单击按钮时,需要调用相应的选择器。但是,使用 ARC 时,应用程序因 EXC_BAD_ACCESS 消息而崩溃。下面是我的 MainViewController 中的代码

- (void)loadView
{
    [super loadView];
    UIButton *testButton=[UIButton buttonWithType:UIButtonTypeRoundedRect];
    [testButton setFrame:CGRectMake(80,50,160,50)];
    [testButton setTitle:@"Click Me" forState:UIControlStateNormal];
    [testButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:testButton];
}

-(void)buttonClicked:(id)sender
{
    NSLog(@"Button Clicked");
}

我启用了 Zombie Objects 并且能够在调试日志中找到以下消息

2012-02-21 22:46:00.911 test[2601:f803] *** -[MainViewController performSelector:withObject:withObject:]: message sent to deallocated instance 0x6b4bba0

查看上面的消息,在我看来 ARC 过早地释放了我的 MainViewController。 我不确定我在这里做错了什么。如果我遗漏了什么,请告诉我。

提前致谢

【问题讨论】:

  • 如果没有代码,VC 是如何初始化的,是无法判断的。我猜你只是没有强烈的参考。

标签: iphone ios memory-management automatic-ref-counting


【解决方案1】:

请使用strong

@property (strong, nonatomic) MainViewController *mvc;

【讨论】:

  • @user1225102:这很可能是正确的答案。如果没有,那么我们需要查看更多代码。
【解决方案2】:

如果其他人有类似的症状,但在我的例子中使用 Storyboards 和 Segues - 这个答案对我有帮助:

IOS 5 message sent to deallocated instance on alloc command

解决方法是在 viewWillDisappear 期间将我的 MKMapView 的委托设置为 nil。花了很长时间才找到解决方案!

【讨论】:

    猜你喜欢
    • 2015-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-25
    • 2012-03-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多