views:

95

answers:

1

Hi I have a very basic issue of memory management with my UIViewController (or any other object that I create); The problem is that in Instruments my Object allocation graph is always rising even though I am calling release on then assigning them nil.

I have 2 UIViewController sub-classes each initializing with a NIB; I add the first ViewController to the main window like [window addSubView:first.view]; Then in my first ViewController nib file I have a Button which loads the second ViewController like :

-(IBAction)loadSecondView{
     if(second!=nil){ //second is set as an iVar and @property (nonatomic, retain)ViewController2* second; 
         [second release];
         second=nil;
     }
     second=[[ViewController2 alloc]initWithNibName:@"ViewController2" bundle:nil];
     [self.view addSubView:second.view];

}

In my (second) ViewController2 i have a button with an action method

-(IBAction) removeSecond{

    [self.view removeFromSuperView];
}

Please let me know if the above scheme works in a managed way for memory...? In Instruments It does not show release of any allocation and keeps the bar status graph keeps on rising.

+1  A: 

First of all, why use this scheme when second is a property:

if(second!=nil){
     [second release];
     second=nil;
 }
 second=[[ViewController2* second]initWithNibName:@"ViewController2" bundle:nil];

A propery automatically releases it's old value when the setter is used. So this could be rewritten as:

if(self.second == nil) { //Prevents creating a new controller if there already is one.
     self.second = [[[ViewController2 alloc] initWithNibName:@"ViewController2" bundle:nil] autorelease];
}

Also, what's up with [ViewController2* second]? Why is that asterisk there, and what does the class method second do?

Rengers
sorry that was just an alloc call....i miss typed it....thanks for the advice... correct me if i m wrong but what i have understood from ur theory is that If i go on allocing the iVAr set as a property, it will release the previous allocation by itself ?
jAmi
Yes, a property will always (afaik) release it's previous value when the setter is invoked. When you define the property as `(retain)`, it will release the old object, and retain the new object.Beware though: when doing this: `self.someStringProperty = [[NSString alloc] init]`, the property will retain the new value, so you have a retain count of two. `self.someStringProperty = [[[NSString alloc] init] autorelease]` will prevent this.
Rengers
ok i tried your suggestion but if(self.second != nil) { //Prevents creating a new controller if there already is one. self.second = [[[ViewController2 alloc] initWithNibName:@"ViewController2" bundle:nil] autorelease];}never gets called so i changed the if condition to if(self.second==nil)which works but then again my graph in Object Allocations is still rising.
jAmi
This is what i did in my (first) ViewController1 button action method-(IBAction) loadSecond{ if(second==nil) self.second=[[[ViewController2 alloc]initWithNibName:@"ViewController2" bundle:nil]autorelease]; [self.view addSubview:second.view];}
jAmi
@Rengers: What do you mean by `retain)ViewController2* sceond`?
KennyTM
@jAmi: Yes, sorry it should be `if(self.second==nil)`. I fixed it in the post. Are you sure you are deallocating the view controller?--- @KennyTM: How the hell did that get there? Some copy paste error I guess. Shouldn't be there. Removed it in the post.
Rengers