Reputation:
I'm experiencing some kind of memory management issue. I have a subclass of UIViewController
and I set its view manually in order to have a reference back to the viewController
and to avoid reference cycle I use weak/unowned
.
Now the problem is , if I use unowned
I have a memory leak but if I use weak
I don't have one. I can't figure out why this happens?
update: It seems like it's a bug.
console output:
removing vc
view Controller deinitialized
custom view deinitialized
I'm using 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")
}
}
Upvotes: 3
Views: 393
Reputation: 1127
Xcode 8.2 Release notes:
The Memory Debugger for macOS and the iOS Simulator fixes reporting of false memory leaks for Swift classes containing either fields of type enum, or classes that inherit from certain Objective-C framework classes. (27932061)
Upvotes: 3