Hi!
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...