我刚刚实现了Apple的Reachability代码,现在我的应用以完全不一致和随机的方式崩溃:它可以成功加载20个网页 - 但之后会在第21次崩溃。尝试,或者可能会在第二次之后崩溃。网页加载尝试可达性崩溃应用
仪器/ NSZombies显示了一些奇怪的事情:RefCt变得高达20(!),“Responsible Callers”有几个不同的:[UIRuntimeConnection initWithCoder],[UINib instantiateWithOwner:options ],[NSKeyedUnarchiver _decodeArrayOfObjectsForKey:]等 这是正常的吗?
或者我应该只关注最后的负责任呼叫者? 这些是[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](http://stackoverflow.com/questions/5386160/how-to-enable-nszombie-in-xcode)。然后看看为什么这个对象被释放。 – rckoenes
好的我打开僵尸,它在崩溃时显示-1的RefCt。在“类别”列中,它告诉我导致崩溃的ViewController的名称 - 其中引入Reachability的VC以及调用Reachability方法的名称。奇怪的是,RefCt在减小到0和-1之前变得很高。为什么这样做? ViewController只调用一次,然后关闭并运行它的事情 - 这是在加载网页时检查连接性。它是否反复调用自己一遍又一遍? – sirab333
你需要显示你的代码。 – Eiko