iPhone - Objective-C Memory Leak with SBJsonParser

柔情痞子 提交于 2019-12-23 03:16:14

问题


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

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

回答1:


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?




回答2:


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


来源:https://stackoverflow.com/questions/4983534/iphone-objective-c-memory-leak-with-sbjsonparser

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!