2 ooomiao oOOMIAO 于 2012.12.18 15:21 提问

应用偶尔会出现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崩溃。

请好心人指点怎么解决?

1个回答

abcdeFGh_xyz
abcdeFGh_xyz   2012.12.18 16:53
已采纳

你应该是忘了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];
}
Csdn user default icon
上传中...
上传图片
插入图片