在一个 iOS 应用程序中,我启动了几个后台线程来处理一些从 REST 服务返回的 XML。
我有一些用户在 PeopleParser.m 中的 [self.names addObject: name] 代码行遇到间歇性崩溃。崩溃日志没有说明原因,只有崩溃的代码行。我无法重现崩溃,所以我假设它与内存有关。我的 *xml 对象是否有可能在我的 for 循环完成之前被主线程池释放?请注意,我没有在后台线程中声明自动释放池。
提取器.m
-(void) peopleFetchComplete:(ASIHTTPRequest *)theRequest {
dispatch_async( backgroundQueue, ^{
PeopleParser *xmlParse = [[PeopleParser alloc] initWithContext:[self managedObjectContext]];
[xmlParse parseXMLData:[theRequest responseData] parseError:&parseError];
[xmlParse release];
});
}
人物解析器.h
@interface PeopleParser : NSObject {
NSMutableArray *names;
...
}
-(void)parseXMLData:(NSData *)data parseError:(NSError **)error;
@property (retain, nonatomic) NSMutableArray *names;
PeopleParser.m
-(id)initWithContext:(NSManagedObjectContext *)managedObjContext{
self = [super init];
self.managedObjectContext = managedObjContext;
names = [[NSMutableArray alloc] init];
return self;
}
-(void)dealloc{
[names release];
[managedObjectContext release];
}
@synthesize names;
-(void)parseXMLData {
//documentWithData returns an autorelease object
SMXMLDocument *xml = [SMXMLDocument documentWithData:someNSData error:nil];
NSArray *people = [[document root] children];
for (SMXMLElement *element in people) {
NSString *name = [element attributeNamed:@"personName"];
[self.names addObject: name]; //CRASHES HERE occasionally
}
//do additional stuff with the data
...
}
崩溃日志
异常类型:EXC_CRASH (SIGABRT) 异常代码:0x0000000000000000、0x0000000000000000 线程崩溃:7
最后异常回溯:
0 核心基础 0x3465229e __exceptionPreprocess + 158
1 libobjc.A.dylib 0x3c37a97a objc_exception_throw + 26
2 核心基础 0x3459c8d4 -[__NSArrayM insertObject:atIndex:] + 764
3 MyApp 0x000fee36 -[PeopleParser parseXMLData:parseError:] (PeopleParser.m:62)
4 MyApp 0x000e9b4c __50-[Fetcher peopleFetchComplete:]_block_invoke (Fetcher.m:818)