简体   繁体   中英

Memory Management with ARC, strange behaviour

I declared a Global Object:

YViewController * yViewController

When in App Launch I am calling:

[self methodOne];

The method does this:

-(void)methodOne
{
 yViewController = [[YViewController alloc] initWithNibName:@"YViewController" bundle:nil];
 self.window.rootViewController=yViewController;
}

When a button clicked in YViewController I am calling:

[self methodTwo];

The method does this:

-(void)methodTwo
{
  XViewController * xViewController = [[XViewController alloc] initWithImage:myImage];
  self.window.rootViewController=xViewController;
}

When a back button tapped on XViewController I am calling [self methodOne]; which navigates back to the YViewController .

The issue is, while I am using ARC, I could not flush/release the xViewController object. Also when checking on instruments, the memory of XViewController keep on increasing as I tap back and forth between XViewController and YViewController .

How can I manage memory in this type of situations with ARC?

If the source of XViewController is open, I would have tweaked it to go with navigation. Any ways ..

Okay so, basically you are creating a new object of ViewController every time, and that is what is resulting in the pile of the memory.

You should have 'XViewController * xViewController' and 'YViewController * yViewController' as class variables and your methods should be some thing like this eg.

Look if the object exists, and if it does, don't allocate it again.

-(void)methodTwo
{
 if(xViewController == nil)
 {
   xViewController = [[XViewController alloc] initWithImage:myImage];
 }
  self.window.rootViewController=xViewController;

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM