0

我按照在线教程了解 iOS 设备是否已连接到互联网。它工作得很好,但是每当它被调用时,它会分配越来越多的内存,而且它经常被调用。我也在使用 ARC,并且分析项目回来干净

 - (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;
    if(defaultRouteReachability){
       bool didRetrieveFlags = SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags);
       CFRelease(defaultRouteReachability);

       if (!didRetrieveFlags) { 
          return NO;
       }
       BOOL isReachable = flags & kSCNetworkFlagsReachable;
       BOOL needsConnection = flags & kSCNetworkFlagsConnectionRequired;
       return (isReachable && !needsConnection) ? YES : NO;
    }
    return false;
 }

它似乎在分配SCNetworkFlagsReachableSCNetworkReachabilityCreateWithAddress,即使我 CALL CFRelease 它仍然分配越来越多的内存。

4

0 回答 0