2011-02-13 50 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; 
    }