2010-12-29 12 views
0

初めてNSInvocationを使用しようとしています。下のコードは、stackoverflowの他の回答コードから採用されています。
タイマーは細かい動作しますが、それは実際には有効期限が切れると(animationEnd :) I台無しにしなかった(iphone)nsInvocationのクラッシュに関する質問

 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]; 
} 

でコードを実行したときにそれがクラッシュしますか?
クラッシュログに基づいて、(animationEnd :)での呼び出しのように、私は呼び出しに渡された引数そのものです。
混乱しています。

ありがとうございます。

答えて

1

animationViewを解放しないでください。あなたは決してそれを保持していない。基本的に、このコードでは、呼び出し(所有権を放棄する者)、配列animationViewArray(ビューが削除されたときに所有権を放棄する)、およびanimationViewのスーパービュー(removeFromSuperviewに電話するとすぐに所有権を放棄します)。

あなたはこれらのいずれにも該当しないため、リリースしないでください。