2

There were topics when breakpoint was not hit in the "deinit" method. The solution was to put an executable code inside. Tried that - didn't work.

The code to initiate a ViewController from the first window:

    let vc = self.storyboard?.instantiateController(withIdentifier: "testwindow") as! NSViewController
    self.presentViewControllerAsModalWindow(vc)

It opens a new window with a button which calls the following code on click

    dismissViewController(self)

and here is the deinit code with breakpoints.

deinit code

zombie objects are not enabled in the scheme.

That wouldn't be a problem if the viewcontroller was re-used when the popup appears again, however new instances of the view controller are created every time.

Is there anything to do to make sure the object gets destroyed?

Dilip Tiwari
  • 1,441
  • 18
  • 31
Ruzard
  • 1,167
  • 3
  • 16
  • 33
  • 1
    I'd say you still have one or more strong references pointing to your VC. That way it won't get deinitialized on calling dismiss. – David May 08 '17 at 11:23
  • @dvdblk I don't have any extra code besides I mentioned. That's a test project. Is it possible that storyboard somehow keeps it from being released? – Ruzard May 08 '17 at 11:32
  • 1
    @Ruzard In which class is the call dismissViewController(self) located? The presenter of the view or the presented view. The documentation for the method states that "To dismiss the modal window, call the dismissViewController(_:) method on self (the presenting view controller).". This could be a potential problem in your code. – Rohan Bhale May 12 '17 at 07:09
  • @RohanBhale Apparently I missed that line. You're right. Thank you. If you could post it as a separate answer - I will mark it as solution and give the bounty. – Ruzard May 12 '17 at 08:35
  • @Ruzard Please check my answer. – Rohan Bhale May 12 '17 at 10:46

2 Answers2

1

Apple's documentation for dismissing the controller presented using presentViewControllerAsModalWindow() states that "To dismiss the modal window, call the dismissViewController(_:) method on self (the presenting view controller).". So you might be dismissing the presented controller from the presented controller itself. Calling dismiss from the presenting view controller will help.

Rohan Bhale
  • 1,323
  • 1
  • 11
  • 29
0

Just tried to replicate this following the same code you used with a button in each viewController and hit the breakPoints successfully and also changes the var num from 0 (viewDidLoad) to 1 and back to 0 in deInit:

What is it that you are attempting? When you hit dismiss, is it not calling the deinit method?

// Main ViewController

@IBAction func letsGo(_ sender: UIButton) {
        if let vc = storyboard?.instantiateViewController(withIdentifier: "second") as? SecondViewController {
            self.present(vc, animated: true, completion: nil)
        }
    }

// Second view Controller

class SecondViewController: UIViewController {

    var num = 0

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        num += 1
        print("\(num)")
    }

    @IBAction func dismissTheHype(_ sender: UIButton) {
        self.dismiss(animated: true, completion: nil)
    }

    deinit {
        print("deiniting")

        num -= 1
        doNothing()
        print("printing number \(num)")
    }

    func doNothing(){
        var number = 4
        number += 1
        print("\(number) times")
    }
}
PaulBart1
  • 61
  • 4
  • I tried your code - yes, it does work as intended. However I am not doing it for iOS. I guess there are architectural differences. – Ruzard May 08 '17 at 12:41
  • By the way, the OP was using `presentViewControllerAsModalWindow`, that is, it's modal. – Papershine May 08 '17 at 12:54