views:

403

answers:

1

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

+1  A: 

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

KennyTM
aha, gotcah. I changed it to UIView *tempController = [self.view viewWithTag:700]; and now I don't see any leaks, and no need to release tempController since it's just a pointer to some other view owned by something else. thanks!
Jeof