开发者

Memory leak when using UIwebview

开发者 https://www.devze.com 2022-12-10 20:44 出处:网络
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

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 l-e-a-k-s.

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.

0

精彩评论

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

关注公众号