ARC过早地释放ViewController

时间:2012-02-22 06:53:30

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

我是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。 我不确定我在这里做错了什么。如果我遗失了什么,请告诉我。

提前致谢

2 个答案:

答案 0 :(得分:10)

请使用强大

@property (strong, nonatomic) MainViewController *mvc;

答案 1 :(得分:0)

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

IOS 5 message sent to deallocated instance on alloc command

修复是在viewWillDisappear期间将我的MKMapView的委托设置为nil。花了很多年才找到解决方案!

相关问题