Garren Fitzenreiter
Garren Fitzenreiter

Reputation: 839

Why won't this Alert let my view controller deallocate?

I am trying to completely deallocate my view controller from memory. After hours of testing, I've finally narrowed it down to a UIAlertController staying in memory which keeps my view controller from deallocating.

@objc func logout_click() {
    let alert = UIAlertController(title: "Confirmation", message: "Are you sure you want to log out?", preferredStyle: .alert)
    alert.addAction(UIAlertAction(title: "YES", style: .default, handler: { _ in
        // 'YES' button action
        do {
            try Auth.auth().signOut()
            self.popInit()
        } catch {
            print(error)
        }
    }))
    alert.addAction(UIAlertAction(title: "NO", style: .default, handler: { _ in
        // 'NO' button action
        alert.dismiss(animated: true)
    }))
    self.present(alert, animated: true)
}
func popInit() {
    //Go back to init screen
    self.navigationController?.popToRootViewController(animated: true) 
}

As long as this alert doesn't show, I can use popInit() and my view controller deallocates just fine, but after this alert shows up, even after dismissing, the view controller will not deallocate. I am not referencing any variables outside the scope of this function, so why does this not allow me to deallocate? What do I need to do to allow my view controller to deallocate?

Upvotes: 1

Views: 102

Answers (1)

matt
matt

Reputation: 534893

Have the YES action handler declare [weak self] and call self?.popInit().

Also, as suggested in a comment, you can replace the NO handler with nil.

Upvotes: 1

Related Questions