Xcode memory graph debugger not showing cycles

1.3k Views Asked by At

I have been using the Xcode memory graph debugger to find cyclic references in our project and I've found a few of them.

However, I haven't been able to see the cycles in the graph. Only by inspecting the code.

For instance I'll see ...

ViewControllerA ---[parentViewController]---> ViewControllerB

But in code they are created like ...

class ViewControllerA: UIViewController {
    let parentViewController: UIViewController
}

and...

class ViewControllerB: UIViewController {
    let otherViewController: UIViewController!

    viewDidLoad() {
        ...
        otherViewController = ViewControllerA(parentViewController: self)
    }
}

Clearly this is a cyclic reference. But it only shows one arrow in the graph.

Is there a way to make this show both arrows in the graph?

Just created an example...

New Project - Single view - Edit ViewController.swift to...

import UIKit

class ViewController: UIViewController {

    var other: ViewControllerB!

    override func viewDidLoad() {
        super.viewDidLoad()
        other = ViewControllerB(other: self)
    }

}

class ViewControllerB: UIViewController {
    let other: UIViewController

    init(other: UIViewController) {
        self.other = other
        super.init(nibName: nil, bundle: nil)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

In Memory Graph Debugger...

Focus on ViewController...

enter image description here

Focus on ViewControllerB...

enter image description here

From these I can infer that there is a reference cycle. But there are tutorials on the web where it actually shows the cycle with arrows following a cycle around the objects...

Like this from Use your loaf

enter image description here

1

There are 1 best solutions below

1
Kris Markel On

Reference cycles only appear in the memory graph debugger for leaked objects. Because ViewController is referenced by UIWindow, it's not considered leaked, so the cycle between the view controllers isn't displayed.