IOS:坠毁在64位设备 [英] ios:crash in 64 bit device
问题描述
有人能解释为何我的应用程序崩溃,并显示以下错误:
Can someone explain why my app crashes with the following error:
EXC_BAD_ACCESS(code = 1,地址= ...)
EXC_BAD_ACCESS(Code = 1, address= ...)
此崩溃只发生在64位器件。我无法找到答案。
This crash occurs only in 64 bit devices. I am not able to figure it out.
- (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;
}
谢谢!
推荐答案
按照<一个href="https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man3/getifaddrs.3.html"相对=nofollow>文档:
的ifa_addr字段引用任接口的地址或 的链路层地址 接口,如果存在,否则它为NULL。 (该ifa_addr字段的上sa_family字段应 协商,以确定所述ifa_addr地址的格式。)
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.)
很可能有您的64位设备上的一个接口,它不具有 ifa_addr
字段填充。
It is likely there is an interface on your 64-bit device that does not have the ifa_addr
field populated.
要解决您的问题,检查空 ifa_addr
。我还建议终止循环,因为你完成,一旦你找到 EN0
。
To fix your problem, check for a NULL ifa_addr
. I also recommend breaking out of the loop since you are finished once you find 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;
}
...
这篇关于IOS:坠毁在64位设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!