UIButton EXC_BAD_ACCESS error

2019-08-17 01:35发布

问题:

I have an UI button and it works correctly when I pressed. But if I press the button three times, I get an EXc_BAD_ACCESS error. I thought I release something in somewhere but I couldn't find the solution. Could you please help me? Kind regards.

This is the function when I pressed the button. And in dealloc I release them. When I am tracking, it doesn't give the error in function. I got it after function, but I dont know where the code goes after this function.

- (IBAction) doSomething: (id)sender
{
    [self.answerDict replaceObjectAtIndex:currentPageNumber withObject:@"1"];
    [self.b setImage:nil forState:UIControlStateNormal];
    [self.c setImage:nil forState:UIControlStateNormal];
    [self.d setImage:nil forState:UIControlStateNormal];
    [self.e setImage:nil forState:UIControlStateNormal];        

    UIImage *img = [UIImage imageNamed:@"a.jpg"];
    [self.a setImage:img forState:UIControlStateNormal];
    [img release];
}

回答1:

UIImage *img = [UIImage imageNamed:@"a.jpg"];
[self.a setImage:img forState:UIControlStateNormal];
[img release];

[img release]; is the problem. You are releasing an object which you dont own. img in this case is auto-released.

Remove [img release]; and see if the crash occurs



回答2:

I suggest you to comment the code line by line and in that way you will understand what is the purpose of BAD_ACCESS error. At firs close whole code in the doSomething: may be the main reason is in your button ...