DEV Community

Incodable
Incodable

Posted on

Track the view controllers' lifecycle using symbolic breakpoints in Xcode

Imagine you've just started a new job in a company and you are going to work on a project that was developed a long time ago. Unsurprisingly, you are exposed to colossal legacy code with lots of views and classes. The first step is always to understand the code and know which works how. So you start to launch the project and see how it works. But you have no idea which view controller you're seeing and where are you in the application. Things get even worse when you're assigned a task, say a simple bug fix, and you have to start right away. Well, there is a solution to find exactly where you should start digging the code to find and fix the issue.

Symbolic Breakpoints to the Rescue

By adding a simple symbolic breakpoint in your project you'll be able to keep track of the project as you're running and navigating through the app.

1- In Xcode, go to Breakpoint Navigator pane and add a Symbolic Breakpoint

Add symbolic breakpoint

2- In the window that appears set the following parameters:
In Symbol, type the following:

-[UIViewController viewWillAppear:]

Then add a Debugger Action with the following value:

expr -- (void) printf("🚀 %s\n", (char *)object_getClassName($arg1))

This is a simple ObjC expression that prints the current view controller class name.

3- Finally, select Automatically continue after evaluating action to prevent the debugger from pausing the application when the breakpoint happens.

Configure the breakpoint

This method will print the name of the current view controller on its viewWillAppear and let you know where exactly you are in the project. This will prevent you from getting lost in the huge codebases when you are working with the projects that you've just taken ownership of (or you already own!).


Bonus: Memory leak hunter!

Now lets you show how you can know when the view controller is released from the memory. This one is incredibly useful when you want to make sure that the view controllers are deallocated successfully and also will help you to catch the possible retain cycles for example.
You can achieve this by creating another simple Symbolic breakpoint without writing a single line of code inside your project!

1- Add another Symbolic Breakpoint with the following value in Symbol:
-[UIViewController dealloc]

2- The rest will remain the same. We just add a different emoji to know it's for the deallocation of the object:
expr -- (void) printf("❎%s\n", (char *)object_getClassName($arg1))

3- Remember to select Automatically continue after evaluating actions.

You keep navigating the application and if you don't see this print when you leave a view controller, it means that we have a possible memory leak. So you can start investigating the problem comfortably.

Demonstration

Happy coding
Al

Top comments (0)