2015-04-16 28 views
1

有人能解釋爲什麼我的應用程序崩潰,出現以下錯誤:IOS:暴跌64位設備

EXC_BAD_ACCESS(Code = 1, address= ...)

僅在64臺設備,就會出現此崩潰。我無法弄清楚。

- (NSString *)getIPAddress 
    { 
     NSString *address = nil; 
     struct ifaddrs *interfaces = NULL; 
     struct ifaddrs *temp_addr = NULL; 
     int success = 0; 

     // retrieve the current interfaces - returns 0 on success 
     success = getifaddrs(&interfaces); 
     if (success == 0) 
     { 
      // Loop through linked list of interfaces 
      temp_addr = interfaces; 
      while(temp_addr != NULL) 
      { 
       if(temp_addr->ifa_addr->sa_family == AF_INET)// crashes here 
       { 
        // Check if interface is en0 which is the wifi connection on the iPhone 
        if([[NSString stringWithUTF8String:temp_addr->ifa_name] isEqualToString:@"en0"]) 
        { 
         // Get NSString from C String 
         address = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr)]; // crash also here 
        } 
       } 
       temp_addr = temp_addr->ifa_next; 
      } 
     } 
     // Free memory 
     freeifaddrs(interfaces); 
     return address; 
    } 

謝謝!

+0

什麼是你的IOS構建SDK指向?最新?你也編譯arm64構建(s)或arm7,你有沒有嘗試使用應用程序的非64位構建? – ApolloSoftware

+0

@AmitApollo:基礎SDK ios8.2,有效架構是arm64,arm7。在非64位設備中沒有崩潰。崩潰只能在64位設備中看到! – rishu1992

回答

1

按照documentation

The ifa_addr field references either the address of the interface or the link level address of the interface, if one exists, otherwise it is NULL. (The sa_family field of the ifa_addr field should be consulted to determine the format of the ifa_addr address.)

很可能存在不具備ifa_addr場填充了64位設備的接口。

要解決您的問題,請檢查NULL ifa_addr。我也建議你一旦找到en0就完成了循環。

... 
while(temp_addr != NULL) 
{ 
    if(temp_addr->ifa_addr != NULL && temp_addr->ifa_addr->sa_family == AF_INET) 
    { 
     // Check if interface is en0 which is the wifi connection on the iPhone 
     if([[NSString stringWithUTF8String:temp_addr->ifa_name] isEqualToString:@"en0"]) 
     { 
      // Get NSString from C String 
      address = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr)]; 
      break; 
     } 
    } 
    temp_addr = temp_addr->ifa_next; 
} 
... 
+0

那我該如何獲取ios devcie的IP地址? – rishu1992

+0

@ rishu1992已更新。 – Joe