Invalid link causes leak, weak not

I'm having a memory management issue. I have a subclass UIViewController

and I manually set its view to return a reference to viewController

, and to avoid using a reference loop I use weak/unowned

. Now the problem is that if I use unowned

I have a memory leak, but if I use weak

I don't have it. I can't figure out why this is happening?

update: This seems to be a bug.

console output:

removing vc
view Controller deinitialized
custom view deinitialized

      

I am 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")
    }
}

      

+3


source to share


1 answer


Xcode 8.2 Release Notes:



The memory debugger for macOS and the iOS Simulator records spurious memory leaks for Swift classes containing any enumerated fields or classes that inherit from a specific Objective-C class structure. (27932061)

+2


source







All Articles