[swift-users] UINavigationController subclass leaks member variable memory
Jordan Rose
jordan_rose at apple.com
Thu Dec 14 12:08:27 CST 2017
> On Dec 13, 2017, at 23:05, 吴君恺 via swift-users <swift-users at swift.org> wrote:
>
> Hello community,
>
> I just encountered a pretty weird behavior when I subclass `UINavigationController` without overriding any initializers.
>
> Simple Code:
>
> import UIKit
>
> class MyViewController: UINavigationController {
>
> let value: Int = {
> print("member init")
> return 3
> }()
> }
>
> let _ = MyViewController(rootViewController: UIViewController())
>
> output is:
>
> member init
> member init
>
> In fact any member variables declared in this subclass is initialized twice but deinitialized only once.
> This phenomenon only appears when using `init(rootViewController:)`.
>
> CMIW, It looks like Swift somehow treat this initializer as a designated initializer, rather than a convenience one.
>
> Any Ideas?
Your analysis is correct. The problem is that UINavigationController's header file doesn't properly declare which initializers are designated and which are convenience. Fortunately, you're not the first to notice this; it's tracked by the UIKit folks as rdar://problem/27255526. I'll poke them about it.
Thanks for bringing it up!
Jordan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20171214/3a10da16/attachment.html>
More information about the swift-users
mailing list