Testing

Debugging Memory in iOS Apps

By José R.C. Cruz, January 06, 2013

Dangling pointers, double frees, memory leaks, and hogs  when it comes to writing apps for iOS, you've got to avoid these common pitfalls. Here's how!

Figure 11 tracks another double free, this time caused by the demoDoubleFree: action method from Listing Three. The last two spikes on the Track pane for the Allocations module mark the point before the app crashes with an EXC_BAD_ACCESS. Selecting these two spikes reveals the object types created before the crash. The type you want to examine is Malloc-16.

Figure 11.

A drill-down on Malloc-16 lists the pointers  one of which is 0x4b52410, the pointer to the tFoo data structure. A drill-down on 0x4b52410 displays its allocation history. It reveals tFoo getting a reference count of 1 when first allocated in demoDoubleFree:. But doubleFree: disposes of tFoo with a call to free(), decreasing the count to 0. So when demoDoubleFree: tries to dispose of tFoo with free() as well, it will raise the aforementioned exception. Correcting this problem requires removing one of the calls to free().

Figure 12 shows a memory hog in progress.

Figure 12.

The routine is the demoMemoryHog: action method (Listing Ten) creating an array of NSNumber objects. The Track pane showed a large swatch of allocations. Selecting the swatch displays the CFNumber objects, all 10,240 instances, taking up a total of 160 KB of memory. The graph on the Allocations (Net/Overall) column for CFNumber was a solid red.

A drill-down lists the addresses of each object (not shown); another drill-down reveals the allocation history for one CFNumber object (0x4d0f6f0). This memory hog is not serious, because the CFNumber objects are all marked for autorelease. But unless you try to restrict array growth, you will end up with a low-memory condition.

Figure 13 is another memory hog in progress. The routine is the demoMemoryHog: method from Listing Eight. The memory hog appears as a red spike on the Track pane next to the Leaks module. Selecting the spike reveals the 1055 instances of Malloc-16 type still resident in memory. Again, the graph on the Allocations (Net/Overall) column was a solid red for that type.

Figure 13.

A drill-down on Malloc-16 lists the pointer addresses of the instances and the routine that made them. And a drill-down on a pointer (shown here as 0x4b2d9a0) provides details for one instance. Memory consumption is small, a mere 16.48 KB. But if the linked-list grew unimpeded, it could lead to a low-memory condition.

Conclusion

In this article, I showed four common memory problems that can affect an iOS app, how to identify some these problems with Xcode's static analysis feature, and how to prepare an Instruments session and attach it to an iOS app process, as well as how to gather process data and examine that data for memory problems. Ensuring your code is frugal with memory usage always pays off when it comes to developing apps for iOS.

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task.
However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

Video

This month's Dr. Dobb's Journal

This month,
Dr. Dobb's Journal is devoted to mobile programming. We introduce you to Apple's new Swift programming language, discuss the perils of being the third-most-popular mobile platform, revisit SQLite on Android
, and much more!