EXC_BAD_ACCESS with IBACTION

前端 未结 3 1942
春和景丽
春和景丽 2020-12-29 05:06

I have read a lot about this issue but mine still seems to be different somehow. So from what I understood, EXC_BAD_ACCESS occurs with memory management problems.

Th

相关标签:
3条回答
  • 2020-12-29 05:52

    I have a thought. That happened to me ages ago. IN IB, is your View property hooked up to your view ?

    I once unhooked these, and the app never launched.

    Btw, worse comes to worse, start the project again. Not worth these headaches if youve done 2 mins of work thus far.

    welcome to the world of iPhone programming. You may need one of these fairly soon wigsmen.com ;-)

    0 讨论(0)
  • 2020-12-29 06:04

    I was tortured by this for a few hours as well. It turned out to be a memory problem as expected. The controller acting as the target for the button was deallocated. It was the root controller of a navigation controller whose view was added directly to the window. My code looked like this:

    MyController *myController = [[MyController new] autorelease];
    UINavigationController* navController = 
        [[[UINavigationController alloc] initWithRootViewController:myController] autorelease];
    [window addSubview:navController.view];
    

    My assumption was that myController would be retained when it's passed as the root controller of the UINavigationController, but that turned out to be wrong. The solution was to assign the controller to a local variable and release it in dealloc. The interface of the object containing the above code should have:

    ...
    @property (retain, nonatomic) MyController *myController;
    ...
    

    And the implementation:

    self.myController = [[MyController new] autorelease];
    UINavigationController* navController = 
        [[[UINavigationController alloc] initWithRootViewController:myController] autorelease];
    [window addSubview:navController.view];
    
    ...
    
    - (void)dealloc {
        [self.myController release];
        ...
        [super dealloc];
    }
    
    0 讨论(0)
  • 2020-12-29 06:05

    As gammal and others pointed out this is a memory problem. It is to do with the controller reference falling out of scope and so its memory is deallocated.

    If you initiate the controller like this:

    MyController* controller = [[MyController alloc] initWithNibName:@"MyNib" bundle:nil];
    [self.view addSubview:controller.view];
    

    You will be fine if you are not using ARC, because the reference won't be deallocated until it is manually released. So I have had this problem when upgrading projects to use ARC.

    If you initiate the controller as autorelease, or are using ARC then as soon as the scope that controller exists in ends, the garbage collector will deallocate the controller, and button press events will cause bad memory exceptions.

    The way to resolve this is to keep the reference alive, so declare it on the interface, or if it needs external access as a property.

    @interface MyParentController : UIView {
    @private
        MyController* controller;
    }
    

    Then add it like:

    controller = [[MyController alloc] initWithNibName:@"MyNib" bundle:nil];
    [self.view addSubview:controller.view];
    

    If you do want it the memory to be collected later, just set the value to nil.

    0 讨论(0)
提交回复
热议问题