Skip to content

Instantly share code, notes, and snippets.

@iljaiwas
Created June 2, 2017 08:44
Show Gist options
  • Save iljaiwas/a7d198e56b758926854655d30c092ae7 to your computer and use it in GitHub Desktop.
Save iljaiwas/a7d198e56b758926854655d30c092ae7 to your computer and use it in GitHub Desktop.
This code crashes unless CFBridgingRetain is called. Any Ideas why?
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