Best cocoa-touch questions in August 2010

Does it matter when super is called in dealloc?

6 votes
- (void)dealloc {
    [super dealloc];
    [receivedData release]; receivedData = nil;
}

or

- (void)dealloc {
    [receivedData release]; receivedData = nil;
    [super dealloc];
}

Yes, it absolutely maters when [super dealloc] is called. Once [super dealloc] is called, you can no longer rely on the NSObject (or whatever your root class is) machinery to function properly. Afterall, your super-class' -dealloc method should call its superclass' etc. until the root class' -dealloc method is called. At that point, everything that these classes allocate to do their job is potentially gone and you're in undefined territory if you try to use any of it.

Your -dealloc method should always look like

- (void)dealloc
{
   // release my own stuff first

   [super dealloc];
}

Does pushViewController retain the controller?

6 votes

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

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.