开发者社区 问答 正文

应用偶尔会出现Exc_Bad_Access

我的应用在许多线程中通过performSeleactoreOnMain线程方法调用下面的函数:

-(void) showAlert: (NSString *)message{
if ([NSRunLoop currentRunLoop] != [NSRunLoop mainRunLoop]) {
    NSLog(@"<< perform in main thread>>");
    [self performSelectorOnMainThread:@selector(showAlert:) withObject:message waitUntilDone:NO];
}
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Info" message:message delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
}

这个方法只在主线程中调用,然后偶尔会在警报:EXC_BAD_ACCESS崩溃。

展开
收起
爵霸 2016-03-26 10:33:41 2156 分享 版权
1 条回答
写回答
取消 提交回答
  • 你应该是忘了return
    试试下面的办法

    -(void) showAlert: (NSString *)message{
        if ([NSRunLoop currentRunLoop] != [NSRunLoop mainRunLoop]) {
            NSLog(@"<< perform in main thread>>");
            [self performSelectorOnMainThread:@selector(showAlert:) withObject:message waitUntilDone:NO];
            return;
        }
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Info" message:message delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
    }
    2019-07-17 19:15:37
    赞同 展开评论
问答地址: