0

My application crashes when selecting a tab. Here is the code that crashes.

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
if ([viewController isKindOfClass:[UINavigationController class]]) {
    [(UINavigationController*)viewController popToRootViewControllerAnimated:NO];
}


}

This is the code that is the problem. Apparently when I popToRoot controller a message is sent to a dealloced object.

DealElementDisplayController *dealElementDisplayController = [[DealElementDisplayController alloc] initWithStores:storeIds :YES];
            dealElementDisplayController.title = [NSString stringWithFormat:@"%@ Products", store.companyName];
            [self.navigationController pushViewController:dealElementDisplayController animated:YES];

Here is the exception.

Exception Type:  EXC_BAD_ACCESS (SIGSEGV)
Exception Codes: KERN_INVALID_ADDRESS at 0xe0000008
Crashed Thread:  0

Thread 0 name:  Dispatch queue: com.apple.main-thread
Thread 0 Crashed:
0   libobjc.A.dylib                 0x3648af78 objc_msgSend + 16
1   UIKit                           0x320e809c -[UITableView(UITableViewInternal)             _createPreparedCellForGlobalRow:withIndexPath:] + 540
    2   UIKit                           0x320e717a -[UITableView(_UITableViewPrivate) _updateVisibleCellsNow:] + 1070
3   UIKit                           0x320e6904 -[UITableView layoutSubviews] + 200
4   UIKit                           0x3208b0d8 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 176
5   CoreFoundation                  0x34fc81f4 -[NSObject performSelector:withObject:] + 36
6   QuartzCore                      0x36b53a9e -[CALayer layoutSublayers] + 210
7   QuartzCore                      0x36b536b6 CA::Layer::layout_if_needed(CA::Transaction*) + 210
8   QuartzCore                      0x36b5783c CA::Context::commit_transaction(CA::Transaction*) + 220
9   QuartzCore                      0x36b57578 CA::Transaction::commit() + 308
10  QuartzCore                      0x36b7f90a CA::Transaction::flush() + 38
11  QuartzCore                      0x36b7f8dc +[CATransaction flush] + 28
12  UIKit                           0x32096152 _afterCACommitHandler + 46
13  CoreFoundation                  0x3503db14 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 12
14  CoreFoundation                  0x3503bd50 __CFRunLoopDoObservers + 252
15  CoreFoundation                  0x3503c0aa __CFRunLoopRun + 754
16  CoreFoundation                  0x34fbf49e CFRunLoopRunSpecific + 294
17  CoreFoundation                  0x34fbf366 CFRunLoopRunInMode + 98
18  GraphicsServices                0x362bb432 GSEventRunModal + 130
19  UIKit                           0x320b5e76 UIApplicationMain + 

Here is the stack trace from Zombie Profile. I am using ARC.

   0 CoreFoundation ___forwarding___
   1 CoreFoundation _CF_forwarding_prep_0
   2 UIKit -[UITableView(UITableViewInternal) _createPreparedCellForGlobalRow:withIndexPath:]
   3 UIKit -[UITableView(UITableViewInternal) _createPreparedCellForGlobalRow:]
   4 UIKit -[UITableView(_UITableViewPrivate) _updateVisibleCellsNow:]
   5 UIKit -[UITableView layoutSubviews]
   6 UIKit -[UIView(CALayerDelegate) layoutSublayersOfLayer:]
   7 CoreFoundation -[NSObject performSelector:withObject:]
   8 QuartzCore -[CALayer layoutSublayers]
   9 QuartzCore CA::Layer::layout_if_needed(CA::Transaction*)
  10 QuartzCore CA::Context::commit_transaction(CA::Transaction*)
  11 QuartzCore CA::Transaction::commit()
  12 QuartzCore +[CATransaction flush]
  13 UIKit _afterCACommitHandler
  14 CoreFoundation __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__
  15 CoreFoundation __CFRunLoopDoObservers
  16 CoreFoundation __CFRunLoopRun
  17 CoreFoundation CFRunLoopRunSpecific
  18 CoreFoundation CFRunLoopRunInMode
  19 GraphicsServices GSEventRunModal
  20 GraphicsServices GSEventRun
  21 UIKit UIApplicationMain
kgibbon
  • 726
  • 1
  • 15
  • 37
  • 1
    Is the view you're navigating to a UITableViewController? Have you tried putting a break in your - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath method to see if it's rendering? – Matt Hudson Mar 31 '12 at 16:16
  • It never gets there. It makes it to viewWillAppear and then throws the exception. There is nothing in viewWillAppear that could trigger the exception. – kgibbon Mar 31 '12 at 16:57

2 Answers2

0

It was due to calling popToRootViewController when the navigation controller is already on the root controller.

kgibbon
  • 726
  • 1
  • 15
  • 37
0

The reason for exception in your application is not due to issue with following code

   - (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
      {
        if ([viewController isKindOfClass:[UINavigationController class]]) {
        [(UINavigationController*)viewController popToRootViewControllerAnimated:NO];
      }
    }

I think when you are tapping on tab bar, the navigation controller pops DealElementDisplayController, and one of the unallocated/released variable in DealElementDisplayController is released again. This may be the reason for your crash. Check in your -(void)dealloc code in DealElementDisplayController, to ensure you have done everything perfect. Use zombie in instruments to check for exceptions. zombie is well suited to detect exceptions related to memory management.

rakeshNS
  • 4,227
  • 4
  • 28
  • 42