ios:在 64 位设备中崩溃 [英] ios:crash in 64 bit device

查看:19
本文介绍了ios:在 64 位设备中崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能解释为什么我的应用程序崩溃并出现以下错误:

Can someone explain why my app crashes with the following error:

EXC_BAD_ACCESS(代码 = 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;
    }

谢谢!

推荐答案

根据文档:

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.

要解决您的问题,请检查 NULL 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆