0

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)
    }
}

ChildViewController

ParentViewController

rmaddy
  • 314,917
  • 42
  • 532
  • 579
mihatel
  • 846
  • 14
  • 34
  • Did you find an answer to this? I just asked a scarily similar question. Haha https://stackoverflow.com/questions/48988810/xcode-memory-graph-debugger-not-showing-cycles – Fogmeister Feb 26 '18 at 13:53
  • 1
    @Fogmeister No :), but it is great it is not only me :D – mihatel Feb 26 '18 at 14:41

1 Answers1

-1

[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):

                                    V--------------------
RootWindow --> GrandParentVC --> ParentVC --> ChildVC --^

This is not a problem.

Now suppose we replace GrandParentVC. An unreachable cycle is created:

RootWindow --> ANewVC

   V--------------------
ParentVC --> ChildVC --^
BaseZen
  • 8,650
  • 3
  • 35
  • 47
  • 1
    thanks for answer, First of all, the project name is ReatinCycle :), that's why it is "categorized under that name". I understand what you mean by "An unreachable cycle is created:" but after changing root view controller to the new one the graph still doesn't show double linked arrows... – mihatel Aug 28 '17 at 15:01