簡體   English   中英

(iphone)nsInvocation崩潰問題

[英](iphone) nsInvocation crash question

我正在嘗試第一次使用NSInvocation,下面的代碼是從stackoverflow的其他答案代碼中采用的。
計時器可以正常運行,但是當計時器實際到期並在(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];
}

我在哪里弄糟?
基於崩潰日志,看起來(animationEnd :)處的調用是我傳遞給調用的參數本身。
令人困惑的stuf ..

謝謝。

不要發布animationView。 您從未保留過它。 基本上,在給出此代碼的情況下,我們看到三個人可能擁有它:調用(誰將在所有權消失時放棄所有權),名為animationViewArray的數組(當視圖從視圖中刪除時將放棄所有權)以及animationView的超級視圖(誰在調用removeFromSuperview時立即放棄所有權)。

由於這些都不是,因此您不應釋放它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM