Home » Code, featured

iPhone Development Tip : Debugging Memory release

28 October 2009 One Comment

I was struggling with a bug in my latest iPhone application, and frankly i got very frustrated with retain-release concept. I missed good old ( well, not that old ) garbage collector, but following code helped me a lot in understanding the release-retain cycle and getting the backtrace of whats actually going on.

First of all include following files in the code :

#include <execinfo.h>
#include <stdio.h>

and then add following lines to the implementation file (.m) :

- (void)release {
	void* callstack[128];
	int i, frames = backtrace(callstack, 128);
	char** strs = backtrace_symbols(callstack, frames);
	for (i = 0; i &lt; frames; ++i) {
		printf("%s\n", strs[i]);
	}
	free(strs);
	NSLog(@"retainCount = %d", [self retainCount]);
	[super release];
}

Now, look at the console, you will see nice backtrace of release-retain cycle. Hope this helps somebody.

Related posts:

  1. iPhone Development Tip : Custom UINavigationBar More and more developers are creating custom user elements these...
  2. iPhone Dev Tip: How to find out Available/Free Memory The iPhone has limited memory,and as developer its our responsibility...
  3. iPhone Development Tip : How to Dynamically Initialize Class in Objective-C While working on an iPhone project i wanted a way...

1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5.00 out of 5)
Loading ... Loading ...

One Comment »