0

I need to push a view controller if the network is reachable. After the view controller is done, I want the memory to be released. The code I have is

Reachability* reachability = [Reachability reachabilityForInternetConnection];
BOOL isReachable = [reachability isReachable];
if(isReachable) {
    ViewController *ppc = [[ViewController alloc] init];
    [self.navigationController pushViewController:ppc animated:YES];
}

To pop the view controller, I use the following code inside (ViewController.m)

-(void) goBack {
  [self.navigationController popViewControllerAnimated:YES];
}

When I see the memory usage in instruments, I don't see any memory being released when view controller is popped. So I modified the code as below.

@autoreleasepool {
    Reachability* reachability = [Reachability reachabilityForInternetConnection];
    BOOL isReachable = [reachability isReachable];
    if(isReachable) {
        ViewController *ppc = [[ViewController alloc] init];
        [self.navigationController pushViewController:ppc animated:YES];
    }
}

When I run above code, there is a memory drop the moment VC is pushed. But I expect the memory to drop when VC is popped. What am I doing wrong here.

user1191140
  • 1,559
  • 3
  • 18
  • 37
  • 2
    You don't need the autoreleasepool. If the memory isn't being released when you pop the view controller then you probably have a reference cycle holding onto the view controller. – rmaddy Aug 24 '15 at 19:33
  • 1
    The use of the `autorelease` method in your code indicates that you aren't using ARC. – user3386109 Aug 24 '15 at 19:36
  • 1
    @rmaddy There's a difference between the `@autoreleasepool` directive and the `autorelease` **method**. OP is using the `autorelease` **method**, which is prohibited when using ARC. – user3386109 Aug 24 '15 at 20:05
  • 1
    @user3386109 My bad. I was looking at the `@autoreleasepool` line and totally overlooked the call to `autorelease` and as a result I misread your comment. You are correct, this is MRC code, not ARC. – rmaddy Aug 24 '15 at 20:07
  • My bad. The auto release in the view controller init was not intended. I am using ARC. So auoreleasepool is not needed ? – user1191140 Aug 24 '15 at 21:05

0 Answers0