开发者

Problem passing NSError back as a return parameter

I am having a problem passing an NSError object back. The first line of code to access the object (in this case, I inserted an NSLog) causes "EXC_BAD_ACCESS".

Is this because I am not explicitly creating an NSError object, but rather getting one from the NSURLRequest and passing it back? In this particular function (downloadFile:), some errors I want to retrieve from other functions, but I create an NSError on two other occasions in the function.

Any help is appreciated.

Here is the offending code:

-(void)someCode {
NSError *err = nil;

localPool = [[NSAutoreleasePool alloc] init];

if (!iap) {
    iap = [[InAppPurchaseController alloc] init];
}

if (![self.iap downloadFile开发者_Go百科:@"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.
}


I guess your NSError object is autoreleased and put on your localPool. You drained that localPool, thus destroying the NSError.

Do you really need localPool in every method? If not, just remove the localPools.

Also, it looks like you forgot to drain the localPool in someCode. Hopefully you just didn't copy it...

-(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
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜