UIButton EXC_BAD_ACCESS error

不问归期 提交于 2020-01-06 18:37:08

问题


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 ...



来源:https://stackoverflow.com/questions/5712957/uibutton-exc-bad-access-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!