2011-12-02 12 views
0

방금 ​​애플의 도달 가능성 코드를 구현했는데 내 응용 프로그램이 완전히 일관성이없고 임의의 방식으로 충돌하고 있습니다. 연속해서 20 개의 웹 페이지를로드 할 수 있었지만 21 일에는 충돌이있었습니다. 시도하거나, 단지 2 일 후에 충돌 할 수 있습니다. 웹 페이지로드 시도도달 가능성 충돌 응용 프로그램

Instruments/NSZombies는 이상한 것을 보여줍니다. RefCt는 20 자 (!)로 높으며, "Responsible Callers"는 몇 가지 다른 것들입니다 : [UIRuntimeConnection initWithCoder], [UINib instantiateWithOwner : options ], [NSKeyedUnarchiver _decodeArrayOfObjectsForKey :] 등 정상입니까?

아니면 마지막 책임자에게 집중해야합니까? RefCt를 0으로 가져 오는 [UIWindowController transitionViewDidComplete : fromView : toView :] 및 [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]; 

}

+3

세부 사항 사용을 알리는 것이 필요합니다. 'EXC_BAD_ACCESS'는 단지 할당 해제 된 어떤 객체가 호출되었다고 알려줍니다. 첫 번째 [NSZombies 사용] (http://stackoverflow.com/questions/5386160/how-to-enable-nszombie-in-xcode) 그런 다음 해당 객체가 할당 해제 된 이유를 살펴보십시오. – rckoenes

+0

ok 나는 좀비를 켜고 충돌시 -1의 RefCt를 보여줍니다. "Categories"열에는 충돌을 일으키는 ViewController의 이름, 즉 Reachability를 가져온 VC와 Reachability 메서드가 호출 된 곳이 나와 있습니다. 이상한 점은 RefCt가 0으로 감소하기 전에 17로 높아진 다음 -1로 증가한다는 것입니다. 왜 그 일을하는거야? ViewController는 한 번만 호출 된 다음 웹 페이지를로드하는 동안 연결을 확인하는 일을 수행하고 실행합니다. 그것은 반복적으로 스스로를 부름니까? – sirab333

+0

코드를 보여줘야합니다. – Eiko

답변

-1

그것이 작동이 코드를 확인하십시오 : 여기

는 코드입니다.

은 project.Then에 SDK 라이브러리에서 project.Import에서 SystemConfiguration 프레임 워크 개발자 사과에서 Reachability.h 및 Reachability.m 파일을 포함 프로젝트에 다음과 같은 GlobalFunction.h 및 GlobalFunction.m 파일을 추가

//GlobalFunction.h 


#import <Foundation/Foundation.h> 

@class Reachability; 

@interface GlobalFunction : NSObject 
{ 
Boolean internetActive; 
Boolean hostActive; 

Reachability * internetReachable; 
Reachability * hostReachable; 
Reachability * wifiReach; 

} 

@property (readwrite,assign) Boolean internetActive; 
@property (readwrite,assign) Boolean hostActive; 

- (Boolean) checkNetworkStatus; 
- (BOOL)connectedToNetwork; 
@end 


//GlobalFunction.m 

#import "GlobalFunction.h" 
#import "Reachability.h" 
#include <netinet/in.h> 
#import <SystemConfiguration/SCNetworkReachability.h> 

@implementation GlobalFunction 

@synthesize internetActive,hostActive; 
- (id)init 
{ 
self = [super init]; 
if (self) { 
    // Initialization code here. 
} 

return self; 
} 



// Checking Internet Connectivity 
- (Boolean) checkNetworkStatus//:(NSNotification *)notice 
{ 
// called after network status changes 
internetReachable = [[Reachability reachabilityForInternetConnection] retain]; 
[internetReachable startNotifier]; 

// check if a pathway to a random host exists 
hostReachable = [[Reachability reachabilityWithHostName: @"www.apple.com"] retain]; 
[hostReachable startNotifier]; 

NetworkStatus internetStatus = [internetReachable currentReachabilityStatus]; 
switch (internetStatus) 
{ 
    case NotReachable: 
    { 
     //NSLog(@"The internet is down."); 
     //[self ShowMsg:@"The internet connection appears to be offline."]; 
     self.internetActive = NO; 
     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; 

     break; 

    } 
    default : 
     self.internetActive = YES; 
     break; 

} 

NetworkStatus hostStatus = [hostReachable currentReachabilityStatus]; 
switch (hostStatus) 

{ 
    case NotReachable: 
    { 
     //NSLog(@"A gateway to the host server is down."); 
     self.hostActive = NO; 

     break; 

    } 
    case ReachableViaWiFi: 
    { 
     //NSLog(@"A gateway to the host server is working via WIFI."); 
     self.hostActive = YES; 

     break; 

    } 
    case ReachableViaWWAN: 
    { 
     //NSLog(@"A gateway to the host server is working via WWAN."); 
     self.hostActive = YES; 

     break; 

    } 
} 

[hostReachable release]; 
[internetReachable release]; 

return self.internetActive; 
} 

- (BOOL)connectedToNetwork { 
// Create zero addy 
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) 
{ 
    //NSLog(@"Error. Could not recover network reachability flags"); 
    return 0; 
} 
BOOL isReachable = flags & kSCNetworkFlagsReachable; 
BOOL needsConnection = flags & kSCNetworkFlagsConnectionRequired; 
//below suggested by Ariel 
BOOL nonWiFi = flags & kSCNetworkReachabilityFlagsTransientConnection; 
NSURL *testURL = [NSURL URLWithString:@"http://www.apple.com/"]; //comment by friendlydeveloper: maybe use www.google.com 
NSURLRequest *testRequest = [NSURLRequest requestWithURL:testURL cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:20.0]; 
//NSURLConnection *testConnection = [[NSURLConnection alloc] initWithRequest:testRequest delegate:nil]; //suggested by Ariel 
NSURLConnection *testConnection = [[[NSURLConnection alloc] initWithRequest:testRequest delegate:nil] autorelease]; //modified by friendlydeveloper 
return ((isReachable && !needsConnection) || nonWiFi) ? (testConnection ? YES : NO) : NO; 
} 

-(void)dealloc { 
internetReachable=nil; 
hostReachable=nil; 
wifiReach=nil; 

[super dealloc]; 
} 

@end 




------>Just write the code for checking internet connection 
#import<GlobalFunction.m> 

-(void)viewDidLoad 
{ 
if([globalFunc checkNetworkStatus]) 
{ 
    [self ShowAlert:@"Internet Connection appears"]; 
} 
else 
{ 
    [self ShowAlert:@"The Internet connection appears to be offline.."]; 
} 
}