我在回传NSError对象时遇到问题。访问该对象的第一行代码(在本例中,我插入了一个NSLog)导致“EXC_BAD_ACCESS”。
这是因为我没有显式创建一个NSError对象,而是从NSURLRequest获取一个并将其传回去?在这个特定的函数(downloadFile :)中,我想从其他函数中检索一些错误,但是我在函数中的另外两个场合创建了一个NSError。
感谢任何帮助。
以下是有问题的代码:
-(void)someCode {
NSError *err = nil;
localPool = [[NSAutoreleasePool alloc] init];
if (!iap) {
iap = [[InAppPurchaseController alloc] init];
}
if (![self.iap downloadFile:@"XXXXX.plist" withRemoteDirectory:nil withLocalDelete:YES withContentType:@"text/xml" Error:&err] ) {
//"EXC_BAD_ACCESS" on calling NSLog on the next line?
NSLog(@"Error downloading Plist: %@", [err localizedDescription]);
[self performSelectorOnMainThread:@selector(fetchPlistFailed:) withObject:err waitUntilDone:NO];
[localPool drain], localPool = nil;
return NO;
}
//Removed the remainder of the code for clarity.
[localPool drain], localPool = nil;
return YES;
}
-(BOOL)downloadFile:(NSString *)fileName
withRemoteDirectory:(NSString *)remoteDirectory
withLocalDelete:(BOOL)withLocalDelete
withContentType:(NSString *)contentTypeCheckString
Error:(NSError **)error {
UIApplication *app = [UIApplication sharedApplication];
app.networkActivityIndicatorVisible = YES;
NSError *localError = nil;
NSAutoreleasePool *localPool = [[NSAutoreleasePool alloc] init];
NSString *urlString = [NSString stringWithFormat:@"http://XXXXX/%@", fileName];
NSLog(@"Downloading file: %@", urlString);
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *req = [[NSURLRequest alloc] initWithURL:url];
NSHTTPURLResponse *response = nil;
NSData *responseData = [NSURLConnection sendSynchronousRequest:req returningResponse:&response error:&localError];
[req release];
if (response == nil || localError) {
NSLog(@"Error retrieving file:%@", [localError localizedDescription]);
if (error != NULL) {
*error = localError;
//THIS NSLog call works just fine.
NSLog(@"Error copied is:%@", [*error localizedDescription]);
}
[localPool drain], localPool = nil;
app.networkActivityIndicatorVisible = NO;
return NO;
}
//Rest of function omitted for simplicity.
}
答案 0 :(得分:3)
我猜你的NSError
对象是autorelease
d并放在localPool
上。你耗尽了localPool
,从而摧毁了NSError
。
在每种方法中你真的需要localPool
吗?如果没有,请删除localPool
。
此外,您似乎忘记了localPool
中的someCode
。希望你没有复制它......
-(void)someCode {
NSError *err = nil;
localPool = [[NSAutoreleasePool alloc] init];
if (!iap) {
iap = [[InAppPurchaseController alloc] init];
}
if (![self.iap downloadFile:@"XXXXX.plist" withRemoteDirectory:nil withLocalDelete:YES withContentType:@"text/xml" Error:&err] ) {
....
[localPool drain], localPool = nil;
return NO;
}
[localPool drain], localPool = nil; // missing
}