[swift-users] UINavigationController subclass leaks member variable memory

Lincoln Wu wjk_buaa at 163.com
Thu Dec 14 20:16:21 CST 2017


Thanks a lot, Jordan.

> 在 2017年12月15日,上午2:08,Jordan Rose <jordan_rose at apple.com> 写道:
> 
> 
> 
>> On Dec 13, 2017, at 23:05, 吴君恺 via swift-users <swift-users at swift.org <mailto: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 <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/20171215/9cb6ad56/attachment.html>


More information about the swift-users mailing list