Search code examples
iosuiviewcontrollerdealloc

Message sent to dealloc UIViewController with error: EXC_BREAKPOINT (code=EXC_I386_BPT)


I have an app that uses a transition file to flip from page to page. I am using ARC and works just fine on 5.1, but crashes all the time on 4.3 simulator. Looking at the thread and the extended detail from instruments, it points to 2 lines of code (shown below) with the error: EXC_BREAKPOINT (code=EXC_I386_BPT). Looks like the UIViewController is being deallocated. Not sure how to fix this. Thanks in advance for any help you can offer!

@synthesize containerView = _containerView;
@synthesize viewController = _viewController;  //ERROR LINE#1

- (id)initWithViewController:(UIViewController *)viewController
{
    if (self = [super init]) 
    {
      _viewController = viewController;
    }
    return self;
}

- (void)loadView
{
    self.wantsFullScreenLayout = YES;
    UIView *view = [[UIView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame];
    view.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
    self.view = view;

    _containerView = [[UIView alloc] initWithFrame:view.bounds];
    _containerView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
    [self.view addSubview:_containerView];

    [_containerView addSubview:self.viewController.view];
}

- (void)transitionToViewController:(UIViewController *)aViewController
                   withOptions:(UIViewAnimationOptions)options
{    
    aViewController.view.frame = self.containerView.bounds;

    [UIView transitionWithView:self.containerView
                      duration:0.65f
                       options:options
                    animations:^{ [self.viewController.view removeFromSuperview];
                        [self.containerView addSubview:aViewController.view];}
                    completion:^(BOOL finished)
   //ERROR LINE#2                 { self.viewController = aViewController;}];
}

Solution

  • You should not be transitioning views in and out from underneath their respective view controllers. You might get it to work, but it's fragile, at best.

    If you want to future-proof your code, you should be transitioning between view controllers and let them handle their own views, but wrap that in the desired animation if you don't like the default animation. So, you should either:

    1. Just do simple presentViewController or pushViewController to go to the next controller and dismissViewController or popViewController to return; or

    2. In iOS 5, you can do your own container view controller (see session 102 in WWDC 2011 or see the discussion of view controller containment in the UIViewController reference) and then you can transitionFromViewController.

    If we knew more about your app flow, why you're doing what you're doing, we can probably advise you further.