1
votes

using Instrument after my migration to ARC I realise that the transition from screens does not clean memory. example of steps :

1)Home screen A -> game screen B = rising of memory usage 2)Game is finished and I go from screen B back to Home screen A

For step 2, memory usage does not go lower. I'd like to have the memory consumed by screen B being freed when removing screen B from screen ... What should I do to be sure this freeing to happen ?

Going from A to B :

GameVC_iPad *game = [[GameVC_iPad alloc]initWithNibName:@"ClassicGameVC_iPad" bundle:nil]; [self presentViewController:game animated:YES completion:nil];

Going back to A from B is done using this code :

HomeVC_iPad *home = [[HomeVC_iPad alloc]initWithNibName:@"HomeVC_iPad" bundle:nil]; home.modalTransitionStyle = UIModalTransitionStyleCrossDissolve; [self presentViewController:home animated:YES completion:nil];

Any clue ?

3
You aren't really going back but instead simply present another view controller over the existing one...JustSid

3 Answers

2
votes

When you go back to A you should do

[self dismissModalViewControllerAnimated:YES];

What you are currently doing is creating a new view controller wich is wrong and navigate to it another time, so this is what is happening

A presents B wich then you present a new A wich then presents a new B ans so on...

Also note that when you navigate to a new viewController iOS caches some view data, so you will never be able to achieve a perfect memory usage before and after you went back,

0
votes

Don't create a new copy of your home controller. Use dismissViewControllerAnimated:completion: to return to the existing one.

0
votes

If you create a delegate class for b, so lets say for example you called it BDelegate and made the ViewController for A conform to that protocol, then you can easily pass a message back to A that you want B to be removed. So for example you could create :

BDelegate :

@protocol BDelegate <NSObject>

- (void)dismissViewB;

@end

Then change view controller for A (header file) to :

@interface AViewController : UIViewController <BDelegate>

Obviously using the actual name of your view controller in there. In the body of the view controller A, add the following method

- (void)dismissViewB {
    [self dismissViewControllerAnimated:YES completion:NULL];
}

Almost there! Now in your B view controller, wherever you want to actually remove the view, so I assume where you currently have

HomeVC_iPad *home = [[HomeVC_iPad alloc]initWithNibName:@"HomeVC_iPad" bundle:nil];
home.modalTransitionStyle = UIModalTransitionStyleCrossDissolve; 
[self presentViewController:home animated:YES completion:nil];

Replace that with

[delegate dismissViewB];

Now all you need inside view controller B is an instance variable pointing to the delegate of A and to assign it. So in the header of controller B add something like

NSObject<BDelegate> *delegate;

And add the appropriate @property for it and @synthesise it in the body. Then when you create view controller B as you are in your first post, simply add

game.delegate = self

Then if it's all gone well, when you tap the button or do whatever you need to do to remove the view, the view controller A will dismiss it for you :)

Hope this helps