I wrote a String extension function in Swift that takes in a Protocol type. It runs a few checks based on the parameter, and throws errors if the base object does not meet the requirements:
func satisfiesComplexityRequirements(setForthBy policy: SCPasswordComplexity) throws
{
guard isAtLeastThisLong(policy.minLength()) else
{
throw SCPolicyPasswordComplexityError.TooShort
}
let maxLen = policy.maxLength()
if maxLen > 0
{
guard isNoLongerThan(maxLen) else
{
throw SCPolicyPasswordComplexityError.TooLong
}
}
guard doesNotExceedThisNumberOfRepeatedCharacters(policy.maxRepeats()) else
{
throw SCPolicyPasswordComplexityError.TooManyRepeatingCharacters
}
}
Running this code with (Swift) optimization level 'Fast' shows the SCPasswordComplexity object not getting retained, but it does get released. This net loss in retain count causes the object to be deallocated (prematurely), leading to an eventual crash:
Zombies Instrument catching reference to prematurely released object
Why is the SCPasswordComplexity object not getting retained inside the function? The function clearly references it. I have also noticed that the app does not crash when it runs the first throw statement, but it crashes when it runs the other two throw statements.