开发者

regarding potential leak

开发者 https://www.devze.com 2023-02-09 22:22 出处:网络
am getting the potential leak in the following code.(returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];)pls help me to find out.

am getting the potential leak in the following code.(returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];)pls help me to find out.

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

}


You could add autorelease to the creation of the string.

In general, any time you use alloc you need to either use autorelease or else use release.


You have a path that returns before releasing the request.

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


You are alloc-ing returnString without a matching release.

Try having an autoreleased string, like so:

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

精彩评论

暂无评论...
验证码 换一张
取 消