iPhone - Objective-C Memory Leak with SBJsonParser

2019-06-05 07:22发布

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.

UPDATE: 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

enter image description here

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];
    }
}

2条回答
我命由我不由天
2楼-- · 2019-06-05 08:00

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;
    }
查看更多
仙女界的扛把子
3楼-- · 2019-06-05 08:15

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?

查看更多
登录 后发表回答