简体   繁体   中英

iPhone/xCode - EXC_BAD_ACCESS on 4th run of Animation Code

This code works for the first 3 times, and every time without fail, it crashes on the 4th run, with a Thread 1: EXC_BAD_ACCESS (code=1, address=0x4) . I have researched all afternoon, but have come up with nothing, is it that I am not releasing something?

- (void)animateImageView:(UIImageView *)imageViewForAnimation
{
        imageViewForAnimation.alpha = 1.0f;
        CGRect imageFrame = imageViewForAnimation.frame;
        //Your image frame.origin from where the animation need to get start
        CGPoint viewOrigin = imageViewForAnimation.frame.origin;
        viewOrigin.y = viewOrigin.y + imageFrame.size.height / 2.0f;
        viewOrigin.x = viewOrigin.x + imageFrame.size.width / 2.0f;

        imageViewForAnimation.frame = imageFrame;
        imageViewForAnimation.layer.position = viewOrigin;
        [self.view addSubview:imageViewForAnimation];

        // Set up fade out effect
        CABasicAnimation *fadeOutAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
        [fadeOutAnimation setToValue:[NSNumber numberWithFloat:0.3]];
        fadeOutAnimation.fillMode = kCAFillModeForwards;
        fadeOutAnimation.removedOnCompletion = NO;

        // Set up scaling
        CABasicAnimation *resizeAnimation = [CABasicAnimation animationWithKeyPath:@"bounds.size"];
        [resizeAnimation setToValue:[NSValue valueWithCGSize:CGSizeMake(40.0f, imageFrame.size.height * (40.0f / imageFrame.size.width))]];
        resizeAnimation.fillMode = kCAFillModeForwards;
        resizeAnimation.removedOnCompletion = NO;

        // Set up path movement
        CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
        pathAnimation.calculationMode = kCAAnimationPaced;
        pathAnimation.fillMode = kCAFillModeForwards;
        pathAnimation.removedOnCompletion = NO;
        //Setting Endpoint of the animation
        CGPoint endPoint = CGPointMake(250.0f, 10.0f);
        //to end animation in last tab use
        //CGPoint endPoint = CGPointMake( 320-40.0f, 480.0f);
        CGMutablePathRef curvedPath = CGPathCreateMutable();
        CGPathMoveToPoint(curvedPath, NULL, viewOrigin.x, viewOrigin.y);
        CGPathAddCurveToPoint(curvedPath, NULL, endPoint.x, viewOrigin.y, endPoint.x, viewOrigin.y, endPoint.x, endPoint.y);
        pathAnimation.path = curvedPath;
        CGPathRelease(curvedPath);

        CAAnimationGroup *group = [CAAnimationGroup animation];
        group.fillMode = kCAFillModeForwards;
        group.removedOnCompletion = NO;
        [group setAnimations:[NSArray arrayWithObjects:fadeOutAnimation, pathAnimation, resizeAnimation, nil]];
        group.duration = 0.7f;
        group.delegate = self;
        //[group setValue:@"groupAnimation" forKey:@"animationName"];
        [group setValue:imageViewForAnimation forKey:@"imageViewBeingAnimated"];

        [imageViewForAnimation.layer addAnimation:group forKey:@"savingAnimation"];


        [imageViewForAnimation release];

}

Remove the release. My advice works because the object imageViewForAnimation was a parameter of your method and nothing in your method is adding a retain to the object. So it makes sense that the imageViewForAnimation object's retain count was being decremented unnecessarily. Only release when you have retained. Keep those in the same scope.

Even better, use ARC ARC you just go to the menu bar in Xcode, Edit > Refactor > Convert to Objective-C ARC... then follow along carefully. It will remove retains and releases from Obj-C stuff.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM