应用偶尔会出现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崩溃。
请好心人指点怎么解决?
答
你应该是忘了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];
}