@autoreleasepool EXC_BAD_ACCESS

时间:2011-10-28 16:14:16

标签: objective-c cocoa-touch exc-bad-access autorelease

我有一个简单的UIViewControler,当我调用方法 [self performSelectorInBackground:@selector(load)withObject:nil]; 时会导致 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

1 个答案:

答案 0 :(得分:7)

UIKit对象不是线程安全的:您只能在主线程上访问它们。行[myTextView setText:@"LOADING ..."];无法在后台线程中安全执行。

这可能是您收到EXC_BAD_ACCESS错误的原因,也可能不是load方法的其余部分,我无法知道其他可能出错的地方。