使用SCNetwork泄漏的互联网连接状态

时间:2011-10-03 08:40:32

标签: objective-c connection

尝试构建简单的函数来获取Internet状态,但每次调用此函数时都会出现泄漏:

+ (BOOL) connectionStatus
{
    BOOL retVal = NO;
    const char *hostName = [@"google.com"
                            cStringUsingEncoding:NSASCIIStringEncoding];


    SCNetworkReachabilityRef reach = SCNetworkReachabilityCreateWithName(kCFAllocatorDefault, hostName); // Attempt to ping google.com
    SCNetworkConnectionFlags flags;
    SCNetworkReachabilityGetFlags(reach, &flags); // Store reachability flags in the variable, flags.

    if(kSCNetworkReachabilityFlagsReachable & flags) {
        // Can be reached using current connection.
    }

    if(kSCNetworkReachabilityFlagsConnectionAutomatic & flags) {
        // Can be reached using current connection, but a connection must be established. (Any traffic to the specific node will initiate the connection)
    }

    if(kSCNetworkReachabilityFlagsIsWWAN & flags) {
        // Can be reached via the carrier network
    } else {
        // Cannot be reached using the carrier network
    }

    if((kSCNetworkReachabilityFlagsReachable & flags) && !(kSCNetworkReachabilityFlagsIsWWAN & flags)) {
        // Cannot be reached using the carrier network, but it can be reached. (Therefore the device is using wifi)
        retVal = YES;
    } else if (kSCNetworkReachabilityFlagsIsWWAN & flags) {
        // Using the carrier network
        retVal = YES;
    } else {
        // No connection available.
    }

    return retVal;
}

仪器显示泄漏并始终指向来自SystemConfiguration的负责框架SCNetworkReachabilityGetFlags和SCNetworkReachabilityGetFlags。 有什么想法吗?

1 个答案:

答案 0 :(得分:1)

当你完成'到达'时

CFRelease(reach);