我从 servin.com 网站获得了以下代码。尽管代码有效,但 Instruments 报告了内存泄漏。我在代码中看不到任何分配、保留、复制,所以我无法弄清楚如何解决这个内存泄漏。Instruments 的代码和结果如下。任何帮助,将不胜感激。
// Part 1 - Create Internet socket addr of zero
struct sockaddr_in zeroAddr;
bzero(&zeroAddr, sizeof(zeroAddr));
zeroAddr.sin_len = sizeof(zeroAddr);
zeroAddr.sin_family = AF_INET;
// Part 2- Create target in format need by SCNetwork
SCNetworkReachabilityRef target =
SCNetworkReachabilityCreateWithAddress(NULL, (struct sockaddr *) &zeroAddr);
// Part 3 - Get the flags
SCNetworkReachabilityFlags flags;
SCNetworkReachabilityGetFlags(target, &flags);
// Part 4 - Create output
NSString *sNetworkReachable;
if (flags & kSCNetworkFlagsReachable)
sNetworkReachable = @"YES";
else
sNetworkReachable = @"NO";
NSString *sCellNetwork;
if (flags & kSCNetworkReachabilityFlagsIsWWAN)
sCellNetwork = @"YES";
else
sCellNetwork = @"NO";
NSLog (@"Network Reachable: %@", sNetworkReachable);
NSLog (@"Cell Network: %@", sCellNetwork);
仪器的结果...
泄漏对象 - # - 地址 - 大小 - 负责库 - 负责框架
Malloc 16 字节 - 1 - 0x5141d50 - 16 字节 - SystemConfiguration - SCNetworkReachabilityCreateWithAddress
SCNetworkReachability - 1 - 0x51347b0 - 288 字节 - SystemConfiguration - SCNetworkReachabilityCreateWithAddress