Assuming blueViewController is a retain property, a temporary variable is not redundant . Your simplification creates a memory leak. This is a statement from the second leak fragment:
self.blueViewController = [[BlueViewController alloc] initWithNibName:@"BlueView" bundle:nil];
In terms of ownership, you are the owner of the object returned by alloc-init, and then the accessor property claims ownership of the object again, resulting in the object not being saved .
Using a temporary variable solves this problem. Another option is to use autorelease :
self.blueViewController = [[[BlueViewController alloc] initWithNibName:@"BlueView" bundle:nil] autorelease];
Note that after this statement, you actually own the object, and you must free it in dealloc.
You did not specify how the blueViewController property is blueViewController . In any case, regardless of the semantics of the setter ( retain , copy , assign ), this statement is incorrect. I have already explained the most likely scenario: retain . Let's look at two other possibilities (without considering whether they make sense at all):
If blueViewController is a copy property, the expression also blueViewController . The property accessor copies the source object, and now the property contains a pointer to the copy, and you lost the original object, immediately leaking it.
The least likely scenario is that blueViewController is an assign property because it is most likely wrong and you really want retain . But, in any case, you do not have the assign property for objects, for example. delegates, and you should not let them out. You assign your object to it, so either you leak it or you incorrectly release the assign property.
source share