A view controller is one component where memory leak usually takes place since it the one that glues many pieces of code together. One mistake, and you end up leaking.
Apple offers a sophisticated tool for debugging memory leaks like Instrument, but I have to admit that not what I use. When I suspect that there is a leak in my view controller, what I use is put a
If there is a leak,
deinit won't get called, and nothing will be print. Even it's not elegant, but it serves me very well for years.
A better way #
Recently I come across a really nice tip from Cédric Luthi (@0xced).
He uses Symbolic Breakpoint to detect view controllers not being deallocated. The same concept as my
- Go to Breakpoint Navigator (Menu View > Navigators > Show Breakpoint Navigator or ⌘ - command + 8).
- Click + and select Symbolic Breakpoint... or Menu Debug > Breakpoints > Create Symbolic Breakpoint...
- Click Add Action button and set
pop(or any sound you like).
- Add another action by click + next to our sound action.
- Set an action to
Log Messageand set a message to whatever you want to print to the console when a view controller gets deallocated. In my case I set it to
--- dealloc @(id)[$arg1 description]@.
- Check Automatically continue after evaluating actions option since we don't want a debugger to pause when a view controller gets deallocated.
Detect a leak #
With this breakpoint in place, whenever a view controller dismisses or pops out of a navigation stack, you will hear a popping sound and a log in debug console. That's mean everything is working as expected.
If you pop or dismiss a view controller and don't hear the popping sound, that's means there's a leak.
Here are the final Symbolic Breakpoint values.
||Leave blank or
Feel free to follow me on Twitter and ask your questions related to this post. Thanks for reading and see you next time.