Created
June 2, 2017 08:44
-
-
Save iljaiwas/a7d198e56b758926854655d30c092ae7 to your computer and use it in GitHub Desktop.
This code crashes unless CFBridgingRetain is called. Any Ideas why?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private class func createReachabilityManager() -> AFNetworkReachabilityManager? { | |
var localWifiAddress = sockaddr_in() | |
bzero(&localWifiAddress, MemoryLayout.size(ofValue: localWifiAddress)) | |
localWifiAddress.sin_len = UInt8(MemoryLayout.size(ofValue: localWifiAddress)) | |
localWifiAddress.sin_family = sa_family_t(AF_INET) | |
// IN_LINKLOCALNETNUM is defined in <netinet/in.h> as 169.254.0.0 | |
localWifiAddress.sin_addr.s_addr = (IN_LINKLOCALNETNUM).bigEndian | |
guard let reachabilityRef = withUnsafePointer(to: &localWifiAddress, { | |
$0.withMemoryRebound(to: sockaddr.self, capacity: 1) { | |
SCNetworkReachabilityCreateWithAddress(kCFAllocatorDefault, $0) | |
} | |
}) else { | |
return nil | |
} | |
// code crashes if CFBridgingRetain is not called. | |
CFBridgingRetain(reachabilityRef) | |
// the init calls a CFBridgingRelease(reachability) | |
return AFNetworkReachabilityManager.init(reachability: reachabilityRef) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment