How to set a breakpoint to catch UIViewController retain cycle

Important note:
The credit for this tip on how to set a retain cycle breakpoint should be attributed to Cedric Luthi’s who first posted it on Twitter. I am reproducing it here with an updated version for Xcode 10+ so that I can personally refer back to it in the future – as I’d otherwise lose it in my list of tweets.

If you want to know when your View Controllers are deallocated (or more importantly when they are not), this breakpoint logs and plays a sound when the dealloc function is called on any UIViewController, so you can catch retain cycles.

How to set up the breakpoint and identify a retain cycle

  • Start by setting a Symbolic Breakpoint in Xcode and name it what you want
  • Set it on the -[UIViewController dealloc] symbol
  • Module should be UIKitCore if you’re using Xcode 10+ or UIKit for previous versions.
  • Add a sound action and choose the least annoying sound (you’ll be hearing this a lot)
  • Add a log message to print what was deallocated.
    I’ve set mine to ℹ️ Deallocated @(id)[$arg1 description]@ @(id)[$arg1 title]@
  • Tick the Option to automatically continue after evaluating the actions…

If you are dismissing a view controller and the sound doesn’t play, you probably have a retain cycle.

Describes how to set up a symbolic breakpoint to catch UIViewController retain cycles by playing a sound and logging a message when they are deallocated.