2011-10-28 52 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方法的其余部分,我无法知道还有什么可能是错误的。