Memory leak caused by checking protocol conformance in a switch

Originator:oliver.foggin
Number:rdar://40287604 Date Originated:16/05/18
Status:Open Resolved:
Product:iOS/Swift Product Version:
Classification:Bug Reproducible:Always
 
Summary:
A memory leak is caused when checking if a UIViewController subclass conforms to a protocol that is not a class protocol.

This looks like a recurrence (or alternate version) of the bug described here... https://forums.developer.apple.com/thread/15425

I have also given an in depth description of the bug here... https://stackoverflow.com/questions/50355375/why-does-this-stop-a-view-controller-from-deallocating

Steps to Reproduce:
I have created a very small project to show this.

The code is as follows...

```protocol MyProtocol {}

class ViewController: UIViewController, UINavigationControllerDelegate {
    override func viewDidLoad() {
        super.viewDidLoad()
        
        navigationController?.delegate = self
    }
    
    func navigationController(_ navigationController: UINavigationController,
                              animationControllerFor operation: UINavigationControllerOperation,
                              from fromVC: UIViewController,
                              to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        switch (fromVC, toVC) {
        case (is MyProtocol, is MyProtocol):
            print("🍎 NOOOOOO!")
            return nil
        default:
            print("🍏 Yippee!")
            return nil
        }
    }
    
    deinit {
        print(#function, "View Controller")
    }
}

class AnotherViewcontroller: UIViewController, MyProtocol {
    deinit {
        print(#function, "Another View Controller")
    }
}```

The storyboard contains a UINavigationController with rootViewController of `ViewController`. This pushes on to `AnotherViewController`.

Expected Results:
When popping back to `ViewController` the `AnotherViewController` should be deallocated.

Actual Results:
It isn't deallocated. It causes a memory leak.

Version/Build:
Version 9.3.1 (9E501)
iPhone 8 plus simulator

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!