Search code examples
iosswiftuiswitch

UISwitch deinit never called


A few days ago I've posted a problem here and I still havent found an answer for it. I think it's a bug and hope it'll be fixed in the future but can't help but think it might also be something I'm doing wrong myself.

With this question I hope to be able to confirm it's a bug so I can keep continue working on my project without worrying too much about it. If this question is repetitive, I will delete the question right away.

I have subclassed a UISwitch adding a deinit so I can see when it's getting deallocated.

class CustomSwitch: UISwitch { deinit {print("Switch has been deinitialized")} }

Then I've added a property in my view controller:

var customSwitch: CustomSwitch? = CustomSwitch()

To test it, I've added a button with the following function:

func removeSwitch() {
    print(customSwitch)
    customSwitch = nil
    print(customSwitch)
}

And the following prints out in the console:

Optional(<Debugging.CustomSwitch: 0x7f9ec91083c0; baseClass = UISwitch; frame = (0 0; 51 31); layer = <CALayer: 0x6100000384a0>>)
nil

The deinit message is not printed and in instruments it shows the customSwitch is still in memory.

The only thing I could find about this is this


Solution

  • This is (finally) fixed in iOS 10.2