2010-12-29 5 views
0

Я пытаюсь использовать NSInvocation в первый раз, код ниже принят из других кодов ответов в stackoverflow.
Таймер работает нормально, но он выходит из строя, когда он на самом деле истекает, и выполняет код в (animationEnd :)(iphone) nsInvocation crash question

 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 :) - это сам аргумент, который я передал вызову.
непонятный stuf ..

спасибо.

ответ

1

Не выпускать animViewView. Вы никогда не сохранили его. В принципе, учитывая этот код, мы видим трех человек, которые могут его владеть: вызов (который откажется от права собственности, когда он уйдет), массив с именем animationViewArray (который откажется от права собственности, когда представление будет удалено из него) и супервизор animView (который сразу отказывается от права собственности, когда вы звоните removeFromSuperview).

Поскольку вы ничто из этого, вы не должны его выпускать.