Memory Allocation on iPhone
Getting the retain count
I’ve spoken here a few times regarding memory usage on the iPhone platform. It’s a fickle beast at times, but there is a great snippet of code anyone struggling to get to grips with memory management should know. The below code allows you to log (or do anything with) the retain count of an object. This can be used to ensure memory is not leaked or to see if an object is over released causing you app to crash.
- (void)applicationDidFinishLaunching:(UIApplication *)application {
UIViewController *viewController = [[UIViewController alloc] init];
NSLog(@"retainCount:%d", [viewController retainCount]);
[viewController retain];
NSLog(@”retainCount:%d”, [viewController retainCount]);
}
Memory allocation and control is one of the greatest challenges to newcommers so bear this little trick in mind next time you run into issues with memory management.
Similar Posts
- Retain, release. A guide to Cocoa Memory Management
- Retain, release. Part II
- Debugging with NSZombieEnabled














