-[ViewController respondsToSelector:]: message sent to deallocated instance - on tableview scroll

4.3k views Asked by At

Similar Questions has already been asked at SO. My case is little different so I am posting as a new Question.

I have got a scrollView as a main view for controller. It contains two subviews:

  1. Scrollview having a UIView as child.
  2. Tableview containing some numeric Textfields.

I have attached a UITapGestureRecognizer to child scrollview, so that user can dismiss keyboard from any textfield.

UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tableTouched:)];
[topScrollview addGestureRecognizer:tap1];
tap1.delegate = self;

-(void) tableTouched:(UITapGestureRecognizer *)gesture
{
    [portTable endEditing:YES];
    portTable.scrollEnabled = YES;
}

-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
    }

    cell.accessoryType = UITableViewCellAccessoryNone;
    cell.textLabel.font = myFont;
    cell.textLabel.textColor = [UIColor whiteColor];

    switch (indexPath.row) {
        case 0:
        {
            UILabel *tradeLabel = [[UILabel alloc] initWithFrame:CGRectMake(9, 5, 60, 40)];
            tradeLabel.text = @"Trade";
            tradeLabel.font = myFont;
            tradeLabel.textColor = [ColorCodes sharedManager].orangeTextColor;
            tradeLabel.backgroundColor = [UIColor clearColor];
            [cell.contentView insertSubview:tradeLabel aboveSubview:cell.textLabel];
            break;
         }
//similar cases
}
return cell;

}

Now if I keep scrolling tableview for sometime, I get crash with the message mentioned in title. Stack trace using instruments is as follows:

Stack Trace

enter image description here

I can't understand the reason for negative reference count my controller. I haven't set any scrollview delegate, then why reference count becomes -1?

I have already tried attaching gesture to main scrollview, tableview itself and to the child UIView, but still getting the same crash. Please someone point me in right direction.. Thanks.

EDIT

Another stack trace I just got:

enter image description here

1

There are 1 answers

0
NightFury On BEST ANSWER

I successfully found the bug. It was because of a custom control in a cell. It was retaining and releasing explicitly being non-ARC (but included in ARC project). Gesture was causing no problem. Removing those lines solved my problem.