(iphone) nsInvocation crash question
i'm trying to use NSInvocation for the first time, code below are adopted from other answer codes at stackoverflow.
The timer runs fine, but it crashes when it actually expires and executes code at (animationEnd:) UIImageView* animationView = [animationViewArray objectAtIndex: i];
[self.imageView addSubview: animationView];
[animationView startAnimating];
// [NSTimer scheduledTimerWithTimeInterval: 5.5 target: self selector: @selector(animationEnd:) userInfo: animationView repeats: NO];
SEL selector = @selector(animationEnd:);
NSMethodSignature *signature = [self methodSignatureForSelector:selector];
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
[invocation setSelector:selector];
//The invocation object must retain its arguments
// when passing to timer, it's ok
// [animationView retain];
//Set the arguments 开发者_运维知识库
[invocation setTarget:self];
[invocation setArgument:&animationView atIndex:2];
[NSTimer scheduledTimerWithTimeInterval:0.1 invocation:invocation repeats:NO];
-(void) animationEnd:(NSInvocation*) invocation
{
UIImageView* animationView = nil;
[invocation getArgument:&animationView atIndex:2];
[animationView removeFromSuperview];
[animationView release];
}
Where did I mess up?
Based on crash log, looks like invocation at (animationEnd:) is the argument itself I passed to the invocation. confusing stuf..Thank you.
Don't release animationView. You never retained it. Basically, given this code, we see three people who may own it: the invocation (who will relinquish ownership when it goes away), the array named animationViewArray
(who will relinquish ownership when the view gets removed from it), and the animationView's superview (who relinquishes ownership for it immediately when you call removeFromSuperview
).
Since you are none of these, you should not release it.
精彩评论