0

在我将 Reachability .h 和 .m 文件合并到它以检查互联网连接之前,我的应用程序运行良好。虽然在大多数情况下一切仍然运行良好(所有网页都正确加载,并且当我故意关闭我的机场以进行测试时,可达性优雅地捕捉到它) - 我现在确实在奇怪的时间遇到​​了莫名其妙的崩溃,可怕的“ Exc-Bad-Access" 错误...我运行 Instruments 并找到了 Zombie - 请参阅屏幕抓取: 注意 RefCt 列 - 计数达到 20!

查看“RefCt”列,您可以看到它达到了 15 - 我已经看到它超过了 20!在“Responsible Caller”列(最右侧)中,我看到了各种我不认识的方法——以及我当然没有发起的呼叫——我假设它们在运行期间被系统内部调用-时间?

无论哪种方式,我都非常仔细地遵循了 Apple 的可达性代码和说明,以及来自这个排名非常高的 stackoverflow 线程的提示: 如何在 iOS 或 OSX 上检查活动的 Internet 连接?

但我仍然遇到这些莫名其妙的崩溃。

任何人都可以提供任何建议吗?

这是代码:

#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:
        {
            [self displayMessageWithTitle:@"ERROR"
                               andMessage:@"Unable To Connect to Internet"
                              andOKButton:@"OK"];
            [self dismissModalViewControllerAnimated:YES];
            break;

        }
        case ReachableViaWiFi:
        {
            NSLog(@"The internet is working via WIFI.");

            break;

        }
        case ReachableViaWWAN:
        {
            NSLog(@"The internet is working via WWAN.");

            break;

        }
    }

    NetworkStatus hostStatus = [hostReachable currentReachabilityStatus];
    switch (hostStatus)
    {
        case NotReachable:
        {
            // NSLog(@"A gateway to the host server is down.");

            [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.");

            break;

        }

        case ReachableViaWWAN:
        {
            NSLog(@"A gateway to the host server is working via WWAN.");

            break;

        }
    }
}



- (void)webViewDidStartLoad:(UIWebView *)webView {
    [activityIndicator startAnimating];
}


- (void)webViewDidFinishLoad:(UIWebView *)webView {
    [activityIndicator stopAnimating];
}


// Since this ViewController is presented Modally, this method removes it 
// via a button click:
-(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];
}


// Was told it might be good to put "removeObserver" here and not in viewWillDisappear
// well neither worked - the App still 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];

}

4

1 回答 1

2

您必须仔细平衡对 addObserver 和 removeObserver 的调用。

如果在 vieWillAppear 中为通知添加观察者,则需要在 viewWillDisappear 中将其移除。

如果您添加了两次观察者,您将被调用两次以获得相同的通知,并且必须调用两次 removeObserver 才能摆脱这两个通知。

我没有看到任何明显的内存管理问题。看起来您发布的所有代码都在创建自动发布的对象,这应该没问题。

于 2011-12-03T02:03:08.027 回答