简体   繁体   中英

Memory leak when using UIwebview

Im in the final stages of my first iphone sdk project. I have been working hard to remove memory leaks from my app, and have mostly succeeded at it. But there I am struggling with one of them. I have a contact screen with a button that fetches a webview, but only if there is a network connection. If not an alert pops up. This works fine in practice but leaks.

All the leaks point to the same place in the code. Here is the first code sample (instruments points to the first of these lines):

BOOL nett=[self connectedToNetwork];
if (!nett)
{
    errorView=[[UIAlertView alloc] initWithTitle:@"Netverksfeil" message:@"Nettet er nede" delegate:self 
                               cancelButtonTitle:@"Filler´n!"   otherButtonTitles:nil];
    [errorView show];
    [errorView release];
}
else{
    iCodeBrowserViewController *browserView=[[iCodeBrowserViewController alloc]initWithNibName:@"iCodeBrowserViewController" bundle:[NSBundle mainBundle]];
    [[self navigationController] pushViewController:browserView animated:YES];
    [browserView release];
}

I suppose that means that the leak is somewhere inside that function...

The next spot instruments points at is in this sample:

// Create zero addy

- (BOOL) connectedToNetwork{  struct sockaddr_in zeroAddress;
bzero(&zeroAddress, sizeof(zeroAddress));
zeroAddress.sin_len = sizeof(zeroAddress);
zeroAddress.sin_family = AF_INET;

// Recover reachability flags
SCNetworkReachabilityRef defaultRouteReachability = SCNetworkReachabilityCreateWithAddress(NULL, (struct sockaddr *)&zeroAddress);
SCNetworkReachabilityFlags flags;

BOOL didRetrieveFlags = SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags);
CFRelease(defaultRouteReachability);

if (!didRetrieveFlags)
{
    printf("Error. Could not recover network reachability flags\n");
    return 0;
}

BOOL isReachable = flags & kSCNetworkFlagsReachable;
BOOL needsConnection = flags & kSCNetworkFlagsConnectionRequired;
BOOL nonWiFi = flags & kSCNetworkReachabilityFlagsTransientConnection;

return ((isReachable && !needsConnection) || nonWiFi) ? 
(([[[NSURLConnection alloc] initWithRequest:[NSURLRequest 
                                            requestWithURL: [NSURL URLWithString:@"http://www.apple.com/"] 
                                            cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:20.0] 
                                  delegate:self]autorelease]) ? YES : NO) : NO;}

This line to be specific:

return ((isReachable && !needsConnection) || nonWiFi) ?

Can any of you see what is leaking in this code? I have copied this part from somewhere else, and managed to alter it slightly. But i must admit i dont understand all the code in that function...

Have you cleaned the project then run a "Build & Analyze"? Most of the time that will tell you about most of your memory issues as long as you've been using Objective C style functions. If you mix-and-match with C style functions it won't be as much help.

I'd guess that the NSURLRequest inside that line is the one that isn't getting released. Might help readability and maintainability to break that line up a bit.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM