关于潜在的泄漏

时间:2011-02-10 11:15:44

标签: iphone

我在下面的代码中遇到了潜在的泄漏。(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;
}

}

3 个答案:

答案 0 :(得分: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 :(得分:1)

您可以在创建字符串时添加autorelease

一般情况下,只要您使用alloc,就需要使用autorelease或使用release

答案 2 :(得分:0)

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

尝试使用自动释放的字符串,如下所示:

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