开发者

How can I avoid a leak with this block-based animation sequence?

开发者 https://www.devze.com 2023-01-04 23:47 出处:网络
I\'m working on animating little subviews throughout my UI using a flip transition. To get the Flip transition to work correctly, I create a temporary UIView to provide the context, run the transition

I'm working on animating little subviews throughout my UI using a flip transition. To get the Flip transition to work correctly, I create a temporary UIView to provide the context, run the transition, and then need to clean up afterwards. But I'm having trouble figuring how to release the object without crashing the application. Here's the code block:

UIView *tempContainer = [UIView alloc];
tempContainer = [self.view viewWithTag:700];

[UIView transitionWithView:tempContainer
        duration:2
        options:UIViewAnimationOptionTransitionFlipFromRight
        animations:^{ 
                    [[[tempContainer subviews] objectAtIndex:0] removeFromSuperview]; 
                    [tempContainer addSubview:newImageView];
                    [newImageView release];
                    } 
            completion:^(BOOL finished){
                    [tempContainer release]; //Crashes app
                    }];

I'm using block-based animation techniques for iOS4. The problem is that my tempContainer开发者_开发问答 is definitely leaking, but if I release or autorelease it in the completion block, my app crashes, and I do it after the [UIView transition...] message, it crashes. What's the best way to refactor this so I don't leak out my memory? (I have 30 of these little things to do.)


It leaks because after you've +alloc-ed

UIView *tempContainer = [UIView alloc];

you immediately override it.

tempContainer = [self.view viewWithTag:700];

It crashes when you -release because you don't own the overriding view ([self.view viewWithTag:700]).

0

精彩评论

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