我遇到了某种内存管理问题。我有一个UIViewController
的子类,我手动设置了它的视图,以便引用回viewController
并避免引用周期我使用weak/unowned
。
现在问题是,如果我使用unowned
我有内存泄漏但是如果我使用weak
我没有。我无法弄清楚为什么会这样?
更新: 这似乎是一个错误。
控制台输出:
removing vc
view Controller deinitialized
custom view deinitialized
我正在使用xcode 8.3.1
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
window = UIWindow(frame: UIScreen.main.bounds)
window?.rootViewController = ViewController(nibName: nil, bundle: nil)
window?.makeKeyAndVisible()
DispatchQueue.main.asyncAfter(deadline: .now() + 5) {
print("removing vc")
self.window?.rootViewController = nil
}
return true
}
class ViewController: UIViewController {
override func loadView() {
view = CustomView(frame: .zero, vc: self)
view.backgroundColor = .red
}
deinit {
print("view Controller deinitialized")
}
}
class CustomView:UIView{
init(frame: CGRect , vc:ViewController) {
self.vc = vc
super.init(frame: frame)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
// weak var vc : ViewController! // no leak
unowned var vc : ViewController // leak
deinit {
print("custom view deinitialized")
}
}
答案 0 :(得分:2)
Xcode 8.2发行说明:
macOS的内存调试器和iOS模拟器修复了报告 包含任一类型字段的Swift类的错误内存泄漏 枚举,或从某些Objective-C框架继承的类 类。 (27932061)