iPhone - SBJsonParser 的 Objective-C 内存泄漏

发布于 2024-10-17 16:41:02 字数 1520 浏览 0 评论 0原文

我使用 Xcode 中的“Leaks”工具不断收到以下内存泄漏信息。由于这是一个库,我只是想知道修复此类泄漏的最佳方法是什么。任何帮助将不胜感激。如果需要,我很乐意分享更多代码。

更新:我找到了这篇文章,它似乎没有希望。有人对如何解决这个问题有任何建议吗?

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

在此处输入图像描述

这就是我使用该库的方式。

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

未央 2024-10-24 16:41:02

通过注释掉 if 的主体(第 50 行),您已经使释放(第 51 行)成为有条件的。也注释掉 if(第 49 行)。

然而,话说回来,你以前的方法也有同样的问题,但显然没有警告,或者也许从未使用过?

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?

信仰 2024-10-24 16:41:02

正如CRD上面所说。您的 JSONFragmentValue 中也有同样的泄漏。这是一个正确的非泄漏版本。

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

或者如果您更喜欢自动释放池。

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

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;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文