Search code examples
alamofire

Will the call to SecTrustCreateWithCertificates cause a memory leak in Alamofire?


In Alamofire,

extension AlamofireExtension where ExtendedType == SecCertificate {
    public var publicKey: SecKey? {
        let policy = SecPolicyCreateBasicX509()
        var trust: SecTrust?
        let trustCreationStatus = SecTrustCreateWithCertificates(type, policy, &trust)

        guard let createdTrust = trust, trustCreationStatus == errSecSuccess else { return nil }

        return SecTrustCopyPublicKey(createdTrust)
    }
}

SecTrustCreateWithCertificates call create a trust object. According to Apple's official documentation:

trust On return, points to the newly created trust management object. Call the CFRelease function to release this object when you are finished with it.

The trust object should be released by calling the CFRelease function (like this: CFRelease(createdTrust)),but it is not called here. Will there be a memory leak?


Solution

  • CoreFoundation objects are automatically memory managed when used from Swift.