我在线阅读了一个教程,了解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;
}
似乎是在SCNetworkFlagsReachable
和SCNetworkReachabilityCreateWithAddress
上进行分配,即使我调用CFRelease它仍然会分配越来越多的内存。