Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does pushViewController retain the controller?

I am struggling to find out if pushViewController retains the controller, currently I have the following code (which works) ...

ColorController *colorController = [[ColorController alloc] initWithNibName:nibColor bundle:nil];
[[self navigationController] pushViewController:colorController animated:YES];
[colorController release];

but am considering removing the release and adding an autorelease ...

ColorController *colorController = [[[ColorController alloc] initWithNibName:nibColor bundle:nil] autorelease];
[[self navigationController] pushViewController:colorController animated:YES];

Much appreciated

Gary

like image 376
fuzzygoat Avatar asked Aug 24 '10 17:08

fuzzygoat


Video Answer


1 Answers

This does nothing...

ColorController *colorController = [[ColorController alloc] initWithNibName:nibColor bundle:nil];
[[[self navigationController] pushViewController:colorController animated:YES] autorelease];

You are autoreleasing the return value of pushViewController:animated:, which is void.

Your first snippet is valid, and correct. pushViewController: does indeed retain the controller that is pushed.

Edit: In your updated code, there is little difference between the two samples. Both maintain proper retain counts. However, it is a "best practice" to avoid using autoRelease unless necessary (especially in a memory sensitive area, like the iPhone). This helps your application to maintain a more predictable and manageable memory footprint.

like image 188
Jerry Jones Avatar answered Oct 11 '22 06:10

Jerry Jones