Definitively, do you have to invalidate() a CADisplayLink when the controller disappears?

A run loop keeps strong references to any display links that are added to it. See add(to:forMode:) documentation:

The run loop retains the display link. To remove the display link from all run loops, send an invalidate() message to the display link.

And a display link keeps strong reference to its target. See invalidate() documentation:

Removing the display link from all run loop modes causes it to be released by the run loop. The display link also releases the target.

So, you definitely have to invalidate(). And if you're using self as the target of the display link, you cannot do this in deinit (because the CADisplayLink keeps a strong reference to its target).


A common pattern if doing this within a view controller is to set up the display link in viewDidAppear and remove it in viewDidDisappear.

For example:

private weak var displayLink: CADisplayLink?

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    startDisplayLink()
}

override func viewDidDisappear(_ animated: Bool) {
    super.viewDidDisappear(animated)
    stopDisplayLink()
}

private func startDisplayLink() {
    stopDisplayLink()  // stop previous display link if one happens to be running

    let link = CADisplayLink(target: self, selector: #selector(handle(displayLink:)))
    link.add(to: .main, forMode: .commonModes)
    displayLink = link
}

private func stopDisplayLink() {
    displayLink?.invalidate()
}

@objc func handle(displayLink: CADisplayLink) {
    // do something
}

Method definition of invalidate():

Removing the display link from all run loop modes causes it to be released by the run loop. The display link also releases the target.

For me this means that displaylink holds the target and run loop holds DispayLink.

Also, according to this link I found, it looks like its rather important to call invalidate() for cleanup of CADisplayLink.

We can actually validate using XCode's wonderful Memory graph debugger:

I have created a test project where a DetailViewController is being pushed on a navigation stack.

class DetailViewController: UIViewController {

    private var displayLink : CADisplayLink?

    override func viewDidAppear() {
        super.viewDidAppear()

        startDisplayLink()
    }

    func startDisplayLink() {
        startTime = CACurrentMediaTime()

        displayLink = CADisplayLink(target: self, 
                                  selector: #selector(displayLinkDidFire))

        displayLink?.add(to: .main, forMode: .commonModes)
    }
}

This initiates CADispalyLink when view gets appeared.

enter image description here

If we check the memory graph, we can see that DetailViewController is still in the memory and CADisplayLink holds its reference. Also, the DetailViewController holds the reference to CADisplayLink.

enter image description here enter image description here

If we now call invalidate() on viewDidDisappear() and check the memory graph again, we can see that the DetailViewController has been deallocated successfully.

This to me suggests that invalidate is a very important method in CADisplayLink and should be called to dealloc CADisplayLink to prevent retain cycles and memory leaks. enter image description here