Table view images never being released

The possible reason here is because of your Settings view controller was not deallocated (not released from memory). No matter how many images are in that view controller when view controller release from memory it will deallocates (remove) all image object from memory.

How to check your view controller is de allocated (release) or not?

Steps:

  1. From Xcode long press on Run button. You get a small popup, Select Profile from that. And you can see new icon replaces the Run icon. (Similarly you can change it Run button to run the application).

enter image description here

  1. Click on this button will start to profile your application.

  2. Select Allocations section in Instruments window. There is a textfield above the allocation listing view. Click on that field and write your view controller name. (In your case SettingViewController).

enter image description here

  1. You can see only filter result related to that key word you just type. Now go to Simulator -> Simulate your flow. Pop back from your view controller and check in Instruments that after leaving that view controller if it’s still in the list. If it is there in a list than your view controller is not release from memory and each time you open that view controller will increase memory and never release until application is running.

How we can de allocate view controller?

I’ll try to explain some way which I know to de allocated controller.

[ 1 ] Override dealloc method in your view controller and release objects in that. Mainly mutable variable and delegates. Put a debug breakpoint on -dealloc method and make sure it is called when you left that controller.

Note: If you have create global variable for class like UIPickerView, UIPopoverController, UIImagePickerController etc. and set delegates for that than these delegates must be nil in -dealloc method.

e.g.

Objective C code:

//---------------------------------------------------------------

#pragma mark
#pragma mark Memory management methods

//---------------------------------------------------------------

- (void) dealloc {
    [self.tableView setDelegate:nil];
    [self.tableView setDataSource:nil];
}

Swift code:

deinit {
    self.tableView.dataSource = nil
    self.tableView.delegate = nil
}

[ 2 ] Global object of subclass also needs to override -dealloc method and release relevant objects and delegates. Similar their dealloc method must have to be called.

e.g.

Check this scenario: Let’s say you have created a subclass of UIView (or any other view) with the name MenuView. And you have created a global variable of this class in your view controller, than it’s dealloc method will be called before view controller’s dealloc method.

@property (nonatomic, strong) MenuView    *menuView;

So here MenuView class needs to be override the -dealloc method and must be called before your view controller’s dealloc method is called.

So that before leaving whole container its child views are release and removed from memory.

Check this in Instruments for MenuView class also that object is still their or not?

If it’s dealloc method is not called than object of MenuView class is still in memory and your view controller class is referencing that object. So even if your view controller’s -dealloc is called it will not deallocated because MenuView class object is live.


[ 3 ] You need to set weak reference to the IBOutlet. e.g.

Objective C code:

@property (nonatomic, weak) IBOutlet UITableView   *tableView;

Swift Code:

@IBOutlet weak var tableView: UITableView!

Feel free to ask if anything is not clear.

Leave a Comment