2011-02-13 37 views
1

我一直使用Xcode中的“泄漏”工具获取以下内存泄漏。由于这是一个图书馆,我只是想知道什么是解决这种泄漏的最佳方法。任何帮助将不胜感激。如果需要,我很高兴分享更多的代码。iPhone - Objective-C内存泄漏与SBJsonParser

更新:我发现这篇文章,似乎并不看好。有没有人有任何建议,如何解决这个问题?

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

enter image description here

这是我如何使用图书馆。

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

回答

6

通过评论你的if(第50行)的主体,你已经使你的发布(第51行)有条件。注释掉if(第49行)。

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

+0

干杯,工作。 – fuzz 2011-02-13 10:16:34

+2

只有一个使用大括号的理由;) – user123444555621 2011-02-13 14:05:15

2

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

或者如果你更喜欢autorelease池。

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