开发者

Multiple release messages cause EXC_BAD_ACCESS crash

First of all i want to start off by saying I'm kind of a beginner in Objective C and the iPhone world and i'm really loving it so far, its really interesting.

I'm coming from the PHP world and trying to adopt some of the very different concepts of Objective C such as needing to release your own objects to avoid memory leaks.

But i'm having a small issue, when i release more than 1 variable at the end of my function, the app crashes with an EXC_BAD_ACCESS error for some reason. I'm sure its something small and stupid but i'm kinda clueless.

Would appreciate your guidance, here's my code:

+ (id) getJsonFromURL: (NSURL *)url withQueryString: (NSString *)queryString withMethod: (NSString *)HTTPMethod error: (NSError **)outError{
    // Initialize request
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
    NSData *requestData = [NSData dataWithBytes:[queryString UTF8String] length: [queryString length]];

    // Set request data and method
    [request setHTTPMethod:HTTPMethod];
    [request setHTTPBody:requestData];

    // Perform request
    NSURLResponse *uResp = nil;
    NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:&uResp error:nil];

    // Make sure HTTP Request was successful (HTTP Code 200)
    NSInteger httpStatus = [((NSHTTPURLResponse *)uResp) statusCode];
    if(httpStatus != 200){
        NSMutableDictionary *userInfo = [NSMutableDictionary dictionary];
        [userInfo setValue:[NSString stringWithFormat:@"The requested URL could not be read (HTTP Code: %d)", httpStatus] forKey: NSLocalizedDescriptionKey];
    }[NSException raise:@"MerchGuru.BadURL" format:@"The URL couldn't be read (HTTP Code: %d)", httpStatus];

    // Decode JSON Output
    NSError *jsonError = nil;
    id respData = [[CJSONDeserializer deserializer] deserialize:returnData error:&jsonError];

    if(jsonError != nil){
        if(outError != nil){
            NSMutableDictionary *userInfo = [NSMutableDictionary dictionary];
            [userInfo setValue:@"The JSON Output provided isn't valid JSON!" forKey:NSLocalizedDescriptionKey];

            *outError = [NSError errorWithDomain:@"com.freak4pc.merchguru" code:60开发者_运维技巧01 userInfo: userInfo];

            [userInfo release];
            NSLog(@"%@", [*outError localizedDescription]);
        }

        [respData release];
        [jsonError release];
        [returnData release];
        [uResp release];
        [requestData release];
        [request release];


        return nil;
    }else{
        [respData release];
        [jsonError release];
        [returnData release];
        [uResp release];
        [requestData release];
        [request release];

        return respData;
    }
}

Thank you :) Shai.


You should not release what you do not own. You only own something if you called alloc or retain on it. In your example, you should only release request


You must only release what you've allocated yourself. Here, you only allocated request, so you have to release just request. All your other variables are created with convenience methods, so they're autoreleased.


You can only release objects that you are init'ing.

There are objects in your code that are autoreleased and your EXC_BAD_ACCESS comes from you releasing those objects.

From the code above, it looks like you only need to release the "request" object.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜