2011-02-10 96 views
0

我得到了下面代碼中的潛在泄漏(returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];)請幫我找出答案。關於潛在的泄漏

+ (NSString *) getResponseFromServer:(NSString *) url 
{ 

NSString *URLString1 = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://www.google.com"]]; 
if(URLString1 == NULL) 
{ 
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Info" message:@"You must be connected to the Internet to use this app. Wireless , 3G and EDGE are supported" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
    alert.delegate = self; 
    [alert show]; 
    [alert release]; 
    return @"NO" ; 
} 

else 
{ 
//CFStringRef escapeChars = (CFStringRef) @":/?#[]@!$&’()*+,;="; 
// url = (NSString *) CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)url, NULL, escapeChars, kCFStringEncodingUTF8); 



url = [url stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]; 
//[self alert:escapedUrlString]; 


url=[url stringByReplacingOccurrencesOfString:@"%2526" withString:@"%26"]; 
//NSLog(@"url 2 = %@ ",url); 
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; 
[request setURL:[NSURL URLWithString:url]]; 
NSLog(@"******Loading URL = %@",[NSURL URLWithString:url]); 

NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; 
NSString *returnString = @""; 
if(returnData) 
{ 
    returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding]; //Potential leak here... 

// NSRange range = [str rangeOfString:supplierSearchText.text options:NSCaseInsensitiveSearch]; 
    //NSLog(@"Searching = %d, %@, %@, %d", i, str, supplierSearchText.text, range.location); 
    //if(range.location == 0) 
    int num = [url rangeOfString:@"iphone_logout.php" options:NSCaseInsensitiveSearch].location; 
       NSLog(@">>>>>>>>> NUmber = %d", num); 
    if(isUserLoggedIn > 0 && global_Timer != nil && num > 1000) 
    { 
    if(((int)([url rangeOfString:@"iphone_user.php" options:NSCaseInsensitiveSearch].location) < 1000) || 
     ((int)([url rangeOfString:@"company_name.php" options:NSCaseInsensitiveSearch].location) < 1000) || 
     ((int)([url rangeOfString:@"company_search.php" options:NSCaseInsensitiveSearch].location) < 1000) || 
     ((int)([url rangeOfString:@"view_web.php" options:NSCaseInsensitiveSearch].location) < 1000)) 
    { 
     [global_Timer invalidate]; 
     NSLog(@">>>>>>>> Timer invalidating >>>>>>>>>>>>>>, %d, %d, %d, %d", ((int)([url rangeOfString:@"iphone_user.php" options:NSCaseInsensitiveSearch].location) > -1) , ((int)([url rangeOfString:@"company_name.php" options:NSCaseInsensitiveSearch].location) > -1) , ((int)([url rangeOfString:@"company_search.php" options:NSCaseInsensitiveSearch].location) > -1), ((int)([url rangeOfString:@"view_web.php" options:NSCaseInsensitiveSearch].location) > -1)); 
     global_Timer = [NSTimer scheduledTimerWithTimeInterval:(NSTimeInterval)300.0 target:global_SupplierToolsViewController selector:@selector(pressButton_Logout) userInfo:nil repeats:NO];   

    } 
    else { 
     NSLog(@"No matches found!, %d", (int)([url rangeOfString:@"iphone_user.php" options:NSCaseInsensitiveSearch].location)); 
    } 
    } 


} 
else //if(!networkFlag) 
{ 
    //networkFlag = YES; 
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"Network connection failed! Try again later!" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
    [alert show]; 
    [alert release]; 
    return @"NO"; 
} 
//[url release]; 
[request release]; 

return returnString; 
} 

} 

回答

0

您正在分配沒有匹配版本的returnString。

嘗試具有自動釋放的字符串,像這樣:

returnString = [NSString stringWithData:returnData encoding:NSUTF8StringEncoding]; 
1

在釋放請求之前,您有一個返回路徑。接受這個答案將有助於你的接受率,並說服更多的人花時間來幫助你。

else //if(!networkFlag) 
{ 
    //networkFlag = YES; 
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"Network connection failed! Try again later!" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
    [alert show]; 
    [alert release]; 
    return @"NO"; 
} 
//[url release]; 
[request release]; 
1

您可以將autorelease添加到字符串的創建中。

一般來說,任何時候您使用alloc您需要使用autorelease或使用release

+0

我也嘗試自動釋放,但它不working..so我在哪裏可以發佈這個「請求」 ..? – Madhumitha 2011-02-10 12:06:27