簡體   English   中英

ARC-如何清除雜物

[英]ARC - How to get rid of stuff

我只是在懷疑某些行為后決定進行測試:

在我的視圖控制器中,我有一個新的游戲方法可以做到這一點:

-(void) newClassicGame {

    if (classicGame!=nil) {
        [classicGame saveStats];
    }

    classicGameController = nil;
    classicGameController = [[RegularGameViewController alloc] initWithPowerMode:NO ];

    classicGame = nil;
    classicGame = [[RegularGame alloc] initWithViewController:classicGameController];

    classicGameController.game = classicGame; // game property is __weak to avoid 
    //retain cycles.

    [classicGame startGame];

}

並測試對象是否被ARC破壞,在classicGame的startGame方法中,我將其放置為:

-(void) startGame
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        NSLog(@"once");
        [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(repeat) userInfo:nil repeats:YES];
    });
//other stuff..

}

-(void) repeat {

    NSLog(@"I repeat");

}

對於那些不熟悉dispatch_once方法的人來說,它只是一種確保在應用程序生命周期中僅一次調用該塊的方法,而不會再次調用該塊。

我啟動我的應用程序,點擊新游戲,然后游戲開始-我看到日志“我重復”每3秒重復一次。”然后回到菜單再次點擊新游戲。奇怪的是,日志不斷重復,表明我的classicGame實例沒有完全銷毀,計時器仍在某處運行,有什么想法嗎?

您創建的計時器保留其目標( self ,游戲),因此至少在計時器無效之前,不會釋放游戲對象。

暫無
暫無

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

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