views:

58

answers:

3
BlueViewController *blueController = [[ BlueViewController alloc] initWithNibName:@"BlueView" bundle:nil];
self.blueViewController = blueController;
[self.view insertSubview:blueController.view atIndex:0];
[blueController release];
+2  A: 

Note this line:

self.blueViewController = blueController;

My guess is that blueViewController property defined with retain attribute, so your object takes ownership of newly created object and then release in the last line we just decrease object's retain count back to 1 to avoid memory leak.

Vladimir
+1  A: 

The release is for the local, temporary reference, which is owned locally because of the alloc. Before releasing, it is passed to the view, which will retain it for its own purposes, and to the object's own blueViewController setter, which will likewise.

walkytalky
+1  A: 

self.blueViewController = blueController; isn't just assignment. It is equal to: [self setBlueViewController]; generated by @synthesize blueViewController instruction at the start of .m file. In this setter method passed blueController is copied or retained (depends on @propertioes options in the .h file). If it was copied - you don't need original one. If it was retained, retain count after self.blueViewController = blueController; equals 2. By releasing blueController they set retain counter to 1.

It is common practice. If you pass "temp" variable to setter or other method with stores referense, those methods retain passed object. So you release it after that. If you temrorary object is autoreleased, then you don't need to release it. Please read Memory Management Guide

OgreSwamp