Search code examples
exceptionuiviewios4uinavigationcontrolleriphone-4

iPhone4 EXC_BAD_ACCESS for UIView after dealloc. How to debug?


I'm working with Apple's Accelerometer Graph Example: http://developer.apple.com/library/ios/#samplecode/AccelerometerGraph/Introduction/Intro.html

I'm pushing 2 Graph Views onto a navigation controller:

  GraphViewController* graphViewController = [[GraphViewController alloc]initWithNibName:@"GraphViewController" bundle:nil];

    [self.navigationController pushViewController:graphViewController animated:YES];
    [graphViewController release];

The graph's are updated by an external method:

     [motionManager startDeviceMotionUpdatesToQueue:motionQueue withHandler:^(CMDeviceMotion *motion, NSError *error) {
...

      if(graphDelegate)
                    {
                        [self performSelectorInBackground:@selector(notifyGraphDelegateWithMotionEvent:) withObject:motion];

                    }

}

, which calls

 [unfiltered addX:filteredValue y:unfilteredvalue z:10]; 

for each graph. The frequency of updates is 20 times per second

When I pop the view from the navigation controller, I get EXC_BAD_ACCESS after [super dealloc]

-(void)dealloc
{
    // Since 'text' and 'current' are weak references, we do not release them here.
    // [super dealloc] will take care to release 'text' as a subview, and releasing 'segments' will release 'current'.
    [segments release];
    [super dealloc];
}

This is a nasty error, and I really don't know how to troubleshoot something like that. It seems to be something about the order in which the views are de-allocated, as the crash happens after the view is popped. Any ideas on how to troubleshoot something like that?


Solution

  • Set NSZombieEnabled, MallocStackLogging, and guard malloc in the debugger. Then, when your App crashes, type this in the gdb console:

    (gdb) info malloc-history 0x543216
    

    Replace 0x543216 with the address of the object that caused the crash, and you will get a much more useful stack trace and it should help you pinpoint the exact line in your code that is causing the problem.

    See this article for more detailed instructions.