Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableView was deallocated while key value observers were still registered with it

I am using MFSideMenu controller for most of the apps which have side menu function. All the apps are working fine on iOS 7 and prior versions but NOT on iPhone 6 devices running iOS 8 (Works fine on iPhone 5 running iOS8).
I cannot seem to figure out what to do to resolve this error that I am receiving. When click on a cell on the LeftMenu UITableView, app crashes and gives this error.

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'An instance 0x7fc492877e00 of class UITableView was deallocated while key value observers were still registered with it. Current observation info: <NSKeyValueObservationInfo 0x7fc492634860>

Note: UITableView is a XIB IBOutlet

This is how the code looks

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.

    // This will remove extra space on top of the tableview
    self.automaticallyAdjustsScrollViewInsets = NO;
    // This will remove extra separators from tableview
    self.tblViewMenu.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];

    // menu titles
    arrMenu = [Config returnArrLeftMenu];

    // check login status
    [self checkLoginStatus];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CustomCellIdentifier = @"CustomCellIdentifier ";

MenuCell *mCell = (MenuCell *)[tableView dequeueReusableCellWithIdentifier: CustomCellIdentifier];

// menu cell
if (mCell == nil) {
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"MenuCell" owner:self options:nil];

    for (id oneObject in nib) if ([oneObject isKindOfClass:[MenuCell class]])
        mCell = (MenuCell *)oneObject;

}

    mCell.selectionStyle = UITableViewCellSelectionStyleBlue;
    mCell.contentView.backgroundColor = [UIColor clearColor];
    mCell.backgroundColor = [UIColor clearColor];
    mCell.lblTitle.text = [arrMenu objectAtIndex:indexPath.row];

    return mCell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    // load selected menu view on to the MAinViewController
    MainViewController *mainController = [[MainViewController alloc] initWithNibName:@"MainViewController" bundle:nil];

    mainController.title = [arrMenu objectAtIndex:indexPath.row];
    mainController.menuIndex = (int)indexPath.row;

    UINavigationController *navigationController = self.menuContainerViewController.centerViewController;
    NSArray *controllers = [NSArray arrayWithObject:mainController];
    navigationController.viewControllers = controllers;
    [self.menuContainerViewController setMenuState:MFSideMenuStateClosed];

}
like image 226
smartsanja Avatar asked Dec 02 '22 17:12

smartsanja


2 Answers

You are likely observing a property change of the TableView with code like this:

[self.tblViewMenu addObserver:self forKeyPath:@"frame" options:NSKeyValueObservingOptionNew context:nil]

The reference to the observer (your class instance) is stored with a unsafe unretained semantic. You will have to clean that up during dealloc() to avoid app crashes (ARC can't handle this for you).

So your dealloc() should deregister the observer like this:

- (void)dealloc {
    [self.tblViewMenu removeObserver:self forKeyPath:@"frame"];
}

You'll have to replace "frame" with the name of the property that your are interested in.

Be sure to check the basic concepts of Key-Value-Observing (KVO) in the proper Apple Guide

EDIT

I just checked the source code of MFSideMenu. There is no sign of KVO-usage in that code, so the crash is likely not caused by that component.

like image 72
Sven Driemecker Avatar answered May 23 '23 17:05

Sven Driemecker


For the swift version you can use the "deinit" method to remove all observers

 deinit {
    self.yourtable.removeObserver(self, forKeyPath: "keyname")
}
like image 42
Rahul K Rajan Avatar answered May 23 '23 15:05

Rahul K Rajan