Mykod
Mykod

Reputation: 687

Memory is not released under ARC

I have an app with a parent View Controller. It has some buttons and when one of them is clicked it presents a Modal View Controller (using XIB). The code I use to present and dismiss the Modal VC is :

iPadViewController *iPadVC = [[iPadViewController alloc] initWithNibName:nil bundle:nil];
        [self presentViewController:iPadVC animated:YES completion:NULL];

and to dismiss :

-(IBAction)Back:(id)sender{
[self.presentingViewController dismissViewControllerAnimated:YES completion:nil];
}

So the problem is, when I dismiss it, it does not release any memory. And if I present it 3/4 times it crashes. I am using ARC, never had similar problems because I tough that ARC did all the memory management job. I do have some backgrounds that need a lot of space and they are not getting released either. I tried :

[backgroundImage removeFromSuperview];
backgroundImage=nil;

The memory does decrease using this code, I'm not sure if the image was released because the next time I press the button the memory increases to almost 300mb and terminates due to "memory pressure". If the VC was released it would work fine because it only uses 160mb on the iPad and 65mb on the iPhone. Also, My image is selected through the Xib utilities :

IMG

And I tried to use Xcode instruments and it does not show any leak (!) but it does show that the allocations are growing and growing every time I click a button.

Allocations with Xcode Tools

and on my ViewController.h:

IBOutlet UIImageView (...);
IBOutlet UIImageView (...);
IBOutlet UILabel (...);
IBOutlet UIButton (...);
IBOutlet NSTimer (...);
IBOutlet UIImage (...);
IBOutlet UIView (...);

-(IBAction)Back:(id)sender;

@property (nonatomic, assign) int ;
@property (nonatomic) IBOutlet UIButton ;
@property (nonatomic) IBOutlet UIButton ;
@property (nonatomic) IBOutlet UIButton ;
@property (nonatomic) IBOutlet UIView ;
@property (nonatomic) IBOutlet UILabel ;

I tried to give all the details about my code, but if you need anything else just let me know, and thank you all so much!

Upvotes: 1

Views: 3631

Answers (2)

If Your images are big please see this question: Problem dealloc'ing memory used by UIImageViews with fairly large image in an UIScrollView

To sum up: when using nib files all the assigned images are loaded using

[UIImage imageNamed]

method which is known to cause huge memory allocations because it always caches the images. Try assigning the images to views in the code with

[UIImage imageWithContentsOfFile:path];

method (it is described in the answer to above question).

Upvotes: 5

Aardvark
Aardvark

Reputation: 598

In the modal view controller:

[self dismissViewControllerAnimated:TRUE completion:Nil];

In the main View controller check it exists first before presenting it:

iPadViewController *iPadVC;

if (iPadVC == Nil)
 iPadVC = [[iPadViewController alloc] initWithNibName:nil bundle:nil];

[self presentViewController:iPadVC animated:YES completion:NULL];

Sounds like iPadVC is always in scope so ARC may not be releasing it.

Upvotes: 0

Related Questions