4
votes

Hi my project is ARC based, I am using UINavigationController make transition between ViewController. I am using profiler to analyse what happening behind the scene with memory. I noticed that when i push a ViewController it allocate memory to all its components and when i pop it its not freeing the allocated memory.

Since i am using ARC i am unable to implement dealloc or release any component. I have analysed in detail and there is no memory leak in my project.

I am not using any strong property to push viewController. Here is how i am pushing ViewController.

viewController *obj = [[viewController alloc] init];
[self.navigationController pushViewController:obj animated:NO];

Any clue whats going on? what should i do to free up the memory that i have consumed. Please advise

1
You can implement a dealloc method with ARC. You just can't call [super dealloc]. You should use dealloc is you need to clean up any resources. Most likely you have a retain cycle in your code. Run the analyzer on your code. - rmaddy
what is the purpose of implementing dealloc since i cannot release any component in it. - abdus.me
There are plenty of other tasks you might need to take care of such as removing notification observers or cleaning up temporary files, etc. If you don't need any kind of cleanup then don't implement a dealloc method. ARC will take care of releasing objects. But it won't release things stuck in a retain cycle. - rmaddy
@rmaddy A question: isn't removing notification observers better to do in viewWillDisappear: when using ARC? I've never used dealloc when using ARC, and my approach has always been removing observers that aren't needed when the view isn't on screen. Hence, viewWillDisappear:. - swiftcode
Lets say you push view controller (vc) B over vc A. This will result in view(Will|Did)Disappear being called on vc A. I still want vc A to get any important notifications set it is updated properly by the time vc B is dismissed. But it depends on your needed. Here's a good rule of thumb - keep things balanced. If you add something in viewWillAppear then remove it in viewDidDisappear. If you add something in init, remove it in dealloc. You get the idea. - rmaddy

1 Answers

0
votes

Your description indicates that you have a retain cycle, which leads to objects not being deallocated. A typical source of retain cycles are properties assigned by loading NIB files (usually declared as IBOutlet).

Two strategies to break them and release the objects:

  1. Declare the property as weak:

    @property (nonatomic, weak) IBOutlet UILabel *statusLabel;
    
  2. Set the property to nil in viewDidUnload:

    - (void)viewDidUnload 
    {
        self.statusLabel = nil;
        [super viewDidUnload];
    }