2011-10-28 42 views
0

我有一個簡單的UIViewControler,當我調用方法[self performSelectorInBackground:@selector(load)withObject:nil];它會導致和EXC_BAD_ACCESS@autoreleasepool EXC_BAD_ACCESS

這裏是UIViewControler.m和UIViewControler.h

#import <UIKit/UIKit.h> 
@interface ViewController : UIViewController 

@property (strong, nonatomic) UITextView *myTextView; 

@end 



#import "ViewController.h" 

@implementation ViewController 

@synthesize myTextView; 

- (id)init { 
    self = [super init]; 
    if (self) { 
     myTextView = [[UITextView alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
     [[self view] addSubview:myTextView]; 
     [self performSelectorInBackground:@selector(load) withObject:nil]; 
    } 
    return self; 
} 

- (void) load { 
    @autoreleasepool { 
     [myTextView setText:@"LOADING ..."]; 
     //DO SOMETHING .... 
    } 
} 

@end 

PS:

該項目採用Objective-C的ARC

+0

什麼是崩潰的堆棧跟蹤? – kperryua

回答

7

UIKit對象不是線程安全的:只能在主線程上訪問它們。行[myTextView setText:@"LOADING ..."];不能安全地在後臺線程中執行。

這可能是也可能不是你得到EXC_BAD_ACCESS錯誤,但沒有看到load方法的其餘部分,我無法知道還有什麼可能是錯誤的。