【发布时间】:2011-12-02 12:58:48
【问题描述】:
我刚刚实现了 Apple 的 Reachability 代码,我的应用程序现在以完全不一致和随机的方式崩溃:它可以连续成功加载 20 个网页 - 但随后在 21 日崩溃。尝试,否则它可能会在第二次之后崩溃。网页加载尝试
Instruments/NSZombies 显示了一些奇怪的东西:RefCt 高达 20(!),“负责任的调用者”有几个不同的:[UIRuntimeConnection initWithCoder]、[UINib instantiateWithOwner: options]、[NSKeyedUnarchiver _decodeArrayOfObjectsForKey: ], ETC。 这正常吗?
或者我应该只关注最后一个负责任的来电者吗? 这些是 [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];
}
【问题讨论】:
-
你需要告诉使用mote细节。
EXC_BAD_ACCESS只是告诉我们调用了一些可能已被释放的对象。首先enable NSZombies。然后看看为什么该对象被释放。 -
好的,我打开了 Zombies,它在崩溃时显示 RefCt 为 -1。在“Categories”列中,它告诉我导致崩溃的 ViewController 的名称 - 它是导入 Reachability 的 VC 以及调用 Reachability 方法的位置。奇怪的是,RefCt 在一直递减到 0 之前达到了 17 的高位,然后是 -1。为什么要这样做? ViewController 只被调用一次,然后它关闭并运行它的事情 - 即在加载网页时检查连接性。它是不是一遍又一遍地递归调用自己?
标签: iphone crash reachability