I'm trying to understand how xcode debugging tool works in terms of detecting retain cycles. I have a simple Parent and Child view controllers both holds references to each other. And after executing app opening closing VC several time, when I open debugging tool it neither shows that there is an issue with retain cycle nor runtime issue. Please find below the code example and attached screenshot of xcode debugging tool
class ViewController: UIViewController {
var child: ChildViewController?
@IBAction func open(_ sender: Any) {
performSegue(withIdentifier: "segueChild", sender: nil)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if (segue.identifier == "segueChild") {
child = segue.destination as? ChildViewController
child?.parentVC = self
}
}
}
class ChildViewController: UIViewController {
var parentVC: ViewController?
@IBAction func close(_ sender: Any) {
dismiss(animated: true, completion: nil)
}
}
[EDIT, the real answer] I think you're simply misreading the visual debugger. Taking a closer look at your screen captures, those memory diagrams are actually categorized under Retain Cycles.
Note however:
To actually waste memory, you'll need to abandon all references to the parent
UIViewController
. As long as the parent remains accessible, both parent and child are accessible from somewhere, even though they have a retain cycle.(If you replace the child VC with a new one, the previous cycle actually broken and replaced by a new one. By constantly updating the
child
VC property, you're not wasting anything either.)Imagine (all arrows are strong):
This is not a problem.
Now suppose we replace GrandParentVC. An unreachable cycle is created: