开发者

EXC_BAD_ACCESS after using function

Here the code :

- (void)addAnswerWithNumber:(NSString *)numberAnswer
{    
    UIButton *aButton = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
    [aButton setBackgroundImage:[UIImage imageNamed:@"roundBlue.png"] forState:UIControlStateNormal];
    [aButton addTarget:self action:@selector(removeAnswer:) forControlEvents:UIControlEventTouchUpInside];
    [aButton setTitle:numberAnswer forState:UIControlStateNormal];
    aButton.enabled = YES;
    aButton.titleLabel.font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:18.0];
    aButton.titleLabel.textColor = [UIColor whiteColor];
    aButton.titleLabel.textAlignment = UITextAlignmentCenter;
    [dropableZone addSubview:aButton];
    [buttonList addObject:aButton];
    [aButton release];
    [aButton release];
    if ([buttonList count] > 0) {
        dropHereLabel.text = @"";
        [self repositionRoundButton];
    }else
    {
        dropHereLabel.text = @"Déposez votre ou vos Réponse(s) ici";
    }

}

- (void)repositionRoundButton
{
    int yPos =  ((890 / 2) - (([buttonList count] * (37 + 10)) / 2));

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:1.5];


    for (int i = 0; i < [buttonList count]; i++) {
        UIButton *aButton = [buttonList objectAtIndex:i];
        aButton.frame = CGRectMake(yPos, (90 /2) - (37/2) , 37, 37);
        yPos = yPos + 47;
    }  
    [UIView commitAnimations];
}

- (void)removeAnswer:(id)sender
{
    UIButton *aButton = sender;

    for (int i = 0; i < [answerList count]; i++) {
        AnswerView *answer = [answerList objectAtIndex:i];

        if ([[aButton titleForState:UIControlStateNormal] isEqualToStri开发者_如何学运维ng:answer.number]) {

            [answer.backGroundImage setImage:[UIImage imageNamed:@"qcmBlueButton.png"]];
            [answer setStateToNull];
        }
    }


    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:1.0f];
    aButton.alpha = 0.0f;
    [UIView commitAnimations];
    [buttonList removeObject:aButton];
    [self repositionRoundButton];
    [aButton removeFromSuperview];
    [aButton release];
}

I've a EXC_BAD_ACCESS on int retVal = UIApplicationMain(argc, argv, nil, nil); when I click on the button create with - (void)addAnswerWithNumber:(NSString *)numberAnswer.

Do you see a problem in my code?

Thank you.


You shouldn't release UIButton * aButton; two times in - (void)addAnswerWithNumber:(NSString *)numberAnswer.

I advice to remove retain call when you are creating aButton and remove all [aButton release]; as your object will be autoreleased.

Also you need to remove in any case line [aButton release]; from method - (void)removeAnswer:(id)sender.


It looks like you are over - releasing aButton. You only alloc it once, so you only need to release it once.

Enabling NSZombies will help to pinpoint such errors in the future.


As everyone suggested you are over releasing the button object. Just remove the retain in the line

UIButton *aButton = [[UIButton buttonWithType:UIButtonTypeCustom] retain];

so that it becomes UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom]; This is an auto-released object so you don't need to release them. Hence remove both the statements that releases aButton

Also you dont own the aButton in the removeAnswer method so do not release the button their either.

I suggest you take a look at the Apple Memory Management reference

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜