views:

280

answers:

3

Hello all,

I have multiple views in my application with respective view controllers. What I am doing is as follows.

Here is the more illustrative code:

- (void)applicationDidFinishLaunching:(UIApplication *)application
{
  View1Controller *viewController1 = [[View1Controller alloc] initWithnibName:@"View1"];
  View2Controller *viewController2 = [[View2Controller alloc] initWithnibName:@"View2"];
  View3Controller *viewController3 = [[View3Controller alloc] initWithnibName:@"View3"];

  [window addSubview:viewController1.view];
  [window makeKeyAndVisible];
}

In View1Controller file:

For Some Button Action

- (IBAction) goTOView2:(id)sender
{
  iPhoneApplicationAppDelegate *appDelegate = (iPhoneApplicationAppDelegate*) [[UIApplication sharedApplication] delegate];
  [appDelegate.window.superView removeFromSuperview];
  [appDelgate.window addSubview: appDelgate.viewController2.view];
}

Similarly for view3

I am retaining all this three view controller in my application delegate. When I want to switch to other view I have the following code.

Don't go on the syntax errors of the code.

This three view controller has multiple view with their own navigation controller which for pushing and popping different views.

My problem is when I run this application using instrument, I see as I switch from one view to another the memory consumption keeps increasing.

Please help and thanks for that in advance.

A: 

Adding and removing views from your app's window is not the recommended way of switching between view controllers. You should be using a UITabBar and letting the UITabBarController handle your view controllers.

The fact that your memory consumption keeps increasing means that you are allocating more objects as your application runs. It probably doesn't have anything to do with the way you're switching views. You're most likely allocating objects and forgetting to release them - causing memory leaks. You should try using the Leaks tool in Instruments to determine which lines of code are causing your problems. (Or post more here!)

Ben Gotow
I am using the memory leak tool and have no leaks in it. Plus when I switch to a view and get back to the previous view, it must show the same memory as before which is not the case, infact memory increases again. Memory leaks are pointed by the red pyramids which I checked for the whole application and have no leaks in it.
Your code sample shows the three view controllers being created as _local_ variables within the applicationDidFinishLaunching: function. Are you allocating a new instance of the view controller each time you switch views? At some point or another, it seems you're allocating a new copy of the view when you think you're re-using an old one.
Ben Gotow
No I am setting as @property(nonatomic,retain) to all the viewController variables. So I just create them once while invoking the object and then just use the view property for adding the respective views as subview. Does it create a new copy when we add them using addSubview method. Also If I was allocating something and not releasing them, Dont you think it would have given me leaks in instrument tool.
A: 

The "leaks" tool usually does not help in cases where you see memory slowly grow, these often are not leaks but objects retained unexpectedly.

In the Object Allocation tool, select the "created and still living" option.

Then select a region in the graph where you see more memory being used, when you expect no new memory to be created. Follow that information to see just what is creating objects you are not expecting..

Also, you say you are setting these view controllers as properties. So that means you set them exactly like this in applicationDidFinishLaunching:

self.viewController1 = viewController1;

Right?

Kendall Helmstetter Gelner
Yes, I am doing the exact thing as you said, but I also release the viewController1. meansView1Controller *aViewController = [[View1Controller alloc] initwithNib:@"View1"];self.viewController1 = aViewController;[aViewController release];I did some more research and found out that the leaks are from quartzcore side as I showed them in the picture of the following question.http://stackoverflow.com/questions/1127792/where-is-my-memory-getting-consumed
You really should add that graphic to this question, you can go back and edit the original question here... also the output for the graph is meaningless to anyone since you have not marked "created and still living", it's impossible to tell what memory use is in that list from items that were properly freed.The release you are doing on the view controller is fine.
Kendall Helmstetter Gelner
A: 

This line looks wrong:

[appDelegate.window.superView removeFromSuperview];

What you want is something like:

[viewController1.view removeFromSuperview];

depending on which view you're moving away from.

Daniel Dickison
well thts what I did in the start but later on I thought, I might be removing the view which is already removed.So I used that part. My previous code was like[appDelegate.viewControoler1.view removeFromSuperview];But still its not affecting the memory consumption part. I wanted to ask do the pop and push of navigation controller affects the memory at the stack. I think what my problem is, the stack of views is not being empty, instead the views are being added on one another and not being removed from the stack. Becoz the memory consumed is being shown by quartz core part.