Я только что реализовал код Apple Reachability, и мое приложение теперь дает сбой совершенно непоследовательным и случайным образом: оно могло успешно загружать 20 веб-страниц подряд, но затем вылетало 21-го числа. попытки, иначе он может выйти из строя уже после 2-й попытки. попытка загрузки веб-страницы
Instruments / NSZombies показывает что-то странное: RefCt достигает 20 (!), А «Ответственные вызывающие» - это несколько разных: [UIRuntimeConnection initWithCoder], [UINib instantiateWithOwner: options], [NSKeyedUnarchiver _decodeArrayOfObjects: и т. Д. . Это нормально?
Или я должен просто сосредоточиться на последних ответственных абонентах? Это [UIWindowController transitionViewDidComplete: fromView: toView:] (который возвращает RefCt к 0) и [UIWebView webView: didFinishLoadForFrame:] (который снижает RefCt до -1)?
Как мне отладить и решить эту проблему?
Вот код:
#import "Reachability.h"
-(void) viewWillAppear:(BOOL)animated
{
// check for internet connection
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkNetworkStatus:) name:kReachabilityChangedNotification object:nil];
internetReachable = [[Reachability reachabilityForInternetConnection] retain];
[internetReachable startNotifier];
// check if a pathway to a random host exists
hostReachable = [[Reachability reachabilityWithHostName: @"www.apple.com"] retain];
[hostReachable startNotifier];
// now patiently wait for the notification
}
-(void) viewDidLoad {
url = [NSURL URLWithString: @"http://www.google.com"];
NSURLRequest *req = [NSURLRequest requestWithURL: url];
[webPageView loadRequest:req];
[super viewDidLoad];
}
-(void) checkNetworkStatus:(NSNotification *)notice
{
// called after network status changes
NetworkStatus internetStatus = [internetReachable currentReachabilityStatus];
switch (internetStatus)
{
case NotReachable:
{
//NSLog(@"The internet is down.");
//self.internetActive = NO; // (That's a BOOL variable)
[self displayMessageWithTitle:@"ERROR"
andMessage:@"Unable To Connect to Internet"
andOKButton:@"OK"];
[self dismissModalViewControllerAnimated:YES];
break;
}
case ReachableViaWiFi:
{
//NSLog(@"The internet is working via WIFI.");
//self.internetActive = YES; //
break;
}
case ReachableViaWWAN:
{
//NSLog(@"The internet is working via WWAN.");
// self.internetActive = YES; // (That's a BOOL variable)
break;
}
}
NetworkStatus hostStatus = [hostReachable currentReachabilityStatus];
switch (hostStatus)
{
case NotReachable:
{
// NSLog(@"A gateway to the host server is down.");
// self.hostActive = NO; // (That's a BOOL variable)
[self displayMessageWithTitle:@"ERROR"
andMessage:@"Host Not Reachable"
andOKButton:@"OK"];
[self dismissModalViewControllerAnimated:YES];
break;
}
case ReachableViaWiFi:
{
//NSLog(@"A gateway to the host server is working via WIFI.");
//self.hostActive = YES; // (That's a BOOL variable)
break;
}
case ReachableViaWWAN:
{
//NSLog(@"A gateway to the host server is working via WWAN.");
// self.hostActive = YES; // (That's a BOOL variable)
break;
}
}
}
- (void)webViewDidStartLoad:(UIWebView *)webView {
[activityIndicator startAnimating];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
[activityIndicator stopAnimating];
}
// Since this ViewController is presented Modally, this method removes it:
-(IBAction) dismissWebTixView:(id)sender {
[self dismissModalViewControllerAnimated:YES];
}
-(void) viewWillDisappear:(BOOL)animated {
NSLog(@"In webForTix's 'viewWillDisappear' method....");
[[NSNotificationCenter defaultCenter] removeObserver:self
name:kReachabilityChangedNotification
object:nil];
}
// Read somewhere that it might be better to put the removeObserver
// call here rather than viewWillDisappear...tried both - still get crashes....
- (void)dealloc
{
//NSLog(@"In webForTix's dealloc method....");
// [[NSNotificationCenter defaultCenter] removeObserver:self
// name:kReachabilityChangedNotification
// object:nil];
NSLog(@"In webForTix's dealloc method - removedObserver...");
[super dealloc];
}
EXC_BAD_ACCESS
просто сообщает нам, что был вызван какой-то объект, который, вероятно, был освобожден. Сначала включите NSZombies. Тогда посмотрите, почему этот объект освобожден. - person rckoenes   schedule 02.12.2011