1

I'm using my physical device and not the simulator.

I'm instantiating a vc using storyboard.instantiateViewController(withIdentifier:) and presenting it modally. I dismiss it using presentingViewController?.dismiss(animated: true, completion: nil). Inside the presented vc I have a print method inside Deinit that never runs.

I went to Instruments > Allocations > Statistics > Allocation Summary > MyApp.ThePresenedController and it shows 2 faces saying something is wrong. When I clicked them it took me to the presenting vc's code where I instantiated the vc to present and highlighted it green. After the presented vc is dismissed it's not removed from the Allocation Summary list. Inside the presented vc there isn't a reference to the presenting vc so it's not a weak var problem.

enter image description here

enter image description here

How come storyboard.instantiateViewController(withIdentifier:) is causing me a problem?

Presenting VC:

@IBAction func forgotPasswordButtonTapped(_ sender: UIButton) {

    let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
    let forgotPasswordVC = mainStoryboard.instantiateViewController(withIdentifier: "ForgotPasswordController") as! ForgotPasswordController
    let navVC = UINavigationController(rootViewController: forgotPasswordVC)
    present(navVC, animated: true, completion: nil)
}

Presented VC:

@IBAction func cancelButtonTapped(_ sender: UIButton) {

    presentingViewController?.dismiss(animated: true, completion: nil)
}

deinit{
    print("I've been dismissed")
}

I'm also using the same storyboard.instantiateViewController(withIdentifier:) code inside AppDelegate and the same 2 faces and highlighted green error is occurring.

AppDelegate didFinishLaunching:

let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)

if userDoesThis {

    // if true this first line will highlight green
    let thisVC: ThisController = mainStoryboard.instantiateViewController(withIdentifier: "ThisController") as! ThisController
    let nav = UINavigationController(rootViewController: thisVC)

} else {

    // if false this first line will highlight green
    let thatVC: ThatController = mainStoryboard.instantiateViewController(withIdentifier: "ThisController") as! ThatController
    let nav = UINavigationController(rootViewController: thatVC)
}

window?.rootViewController = nav
window?.makeKeyAndVisible()
return true
Lance Samaria
  • 17,576
  • 18
  • 108
  • 256
  • Does it change if you change `presentingViewController?`to `presentingViewController!`? You could check with `print("\(type(of: <>))"` what class that actually is to make sure all assumptions are right. – Fabian Jul 19 '18 at 23:12
  • @Purpose I walked out the house. When I get back home I’ll try your suggestion. Thanks – Lance Samaria Jul 19 '18 at 23:22
  • @Purpose what does print("\(type(of: <>))" mean? – Lance Samaria Jul 19 '18 at 23:24
  • `print("\(type(of: presentingViewController!))")` is to check presentingViewController is the one you wanted to call dismiss on. – Fabian Jul 19 '18 at 23:47
  • Why do you think that line is the one causing the problem? The circular reference is being reported as a leak, and leaks are reported *when the object is instantiated* not where it's leaked. You probably have a strong reference to your presented view controller *within the presented view controller itself*. If you can't find another source, do you have a long-lived block within that view controller capturing `self`? – Steven Fisher Jul 20 '18 at 01:34
  • @StevenFisher I’ve read that allocations took you to the line causing the problem. So your saying it takes you to where the problem starts and from there you have to figure it out? If that’s what it it might be my Firebase closures that are causing the problem. I thought I added [weak var] in to everything but maybe I missed something. I’ll check. I’ll find the links that says the green is the actual cause and not the start like your saying. Thank you for the advice – Lance Samaria Jul 20 '18 at 01:56
  • https://docs.swift.org/swift-book/LanguageGuide/AutomaticReferenceCounting.html may help you with closures capturing self strongly. – Fabian Jul 20 '18 at 02:03
  • @StevenFisher you was correct. I overlooked a closure that didn't include [weak self]. Thanks for the help. You taught me something I didn't know. The articles I read said that allocations brought you to the offending line. Wrong they were. Also you should post that as an answer and include the explanation you gave and and the fact it might be closures, I'll accept it. – Lance Samaria Jul 20 '18 at 04:43
  • @Purpose it was a closure causing the problem. I thought the green highlight was the offending line, it just highlights were the problem is occurring. Anyhow thanks for the help and thanks for the print statement. Very helpful! – Lance Samaria Jul 20 '18 at 04:46

1 Answers1

0

As @StevenFisher suggested inside the comments the problem wasn’t the green highlighted line itself but instead was a closure that I overlooked and didn’t declare with [weak self]. I’ve read articles that says that pressing the faceless faces takes you to the offending line of code but Steve pointed it that might not be the issue and it can/will instead take you to where the problem starts. In my situation it let me know that I had a problem somewhere in the file as soon as it was instantiated and not the line itself.

Lance Samaria
  • 17,576
  • 18
  • 108
  • 256