开发者

iPhone - Objective-C Memory Leak with SBJsonParser

I keep getting the following memory leak using the "Leaks" tool in Xcode. As this is a library, I'm just wondering what would be the best way to fix such a leak. Any help would be greatly appreciated. I am happy to share more code if needed.

UP开发者_如何学CDATE: I found this article, which doesn't seem promising. Has anyone got any suggestions as to how to fix this?

http://code.google.com/p/json-framework/issues/detail?id=13

iPhone - Objective-C Memory Leak with SBJsonParser

This is how I'm using the library.

- (void)getFacebookProfileFinished:(ASIHTTPRequest *)request {
    NSString *responseString = [request responseString];
    NSMutableDictionary *responseJSON = [responseString JSONValue]; //memory leak 100%   

    NSString *username;
    NSString *firstName = [responseJSON objectForKey:@"first_name"];
    NSString *lastName = [responseJSON objectForKey:@"last_name"];
    NSString *facebookId = [responseJSON objectForKey:@"id"];
    if (firstName && lastName) {
        username = [NSString stringWithFormat:@"%@ %@", firstName, lastName];
    } else {
        username = @"";
    }

    UIAppDelegate.userSessionId = facebookId;
    UIAppDelegate.userFullName = username;

    if (UIAppDelegate.userSessionId != nil) {
        Service1 *service = [[Service1 alloc] init];
        [service UserExists:self action:@selector(handlerUserExists:) facebookUserId:UIAppDelegate.userSessionId];
        [service release];
    } else {
        [Utility displayAlertMessage:@"There has been an error. Please try again later." withTitle:@"Error"];
        [self logoutCompletely];
    }
}


By commenting out the body of your if (line 50) you've made your release (line 51) conditional. Comment out the if (line 49) as well.

However, having said that your previous method has the same issue but apparently no warning, or maybe it was never used?


As CRD said above. You have the same leak in your JSONFragmentValue. Here is a correct non leaking version.

- (id) JSONFragmentValue
{
    SBJasonParser *jsonParser = [SBJasonParser new];
    id repr = [jsonParser fragmentWithString:self];
    if (repr == nil)
    {
        NSLog(@"JSonFragmentValue failed:%@", [jsonParser ErrorTrace]);
    }
    [jsonparser release], jsonParser = nil;
    return repr;
}

Or if you prefer autorelease pools.

- (id) JSONFragmentValue
    {
        SBJasonParser *jsonParser = [SBJasonParser new] autorelease];
        id repr = [jsonParser fragmentWithString:self];
        if (repr == nil)
        {
            NSLog(@"JSonFragmentValue failed:%@", [jsonParser ErrorTrace]);
        }
        return repr;
    }
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜