[英]Unowned Reference causes leak , weak doesn't
我遇到了某種內存管理問題。 我有一個UIViewController
的子類,我手動設置它的視圖,以便有一個返回viewController
的引用,並避免引用循環我使用weak/unowned
。 現在的問題是,如果我使用unowned
我有內存泄漏,但如果我使用weak
我沒有。 我無法弄清楚為什么會這樣?
更新:好像這是一個bug。
控制台輸出:
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")
}
}
Xcode 8.2發行說明:
用於macOS和iOS模擬器的Memory Debugger修復了包含enum類型字段或從某些Objective-C框架類繼承的類的Swift類的虛假內存泄漏報告。 (27932061)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.