从运行循环中删除CADisplayLink时,游戏有时会崩溃

use*_*282 3 xcode breakpoints ios cadisplaylink swift

每次玩游戏时都不会发生这种情况,也许每5或10次播放一次.当游戏结束时,我从运行循环中移除我的CADisplayLink(我用它来设置游戏区域的动画,有点像Flappy Bird中的管道).然而,在少数情况下,它崩溃了.该行旁边有:

Thread 1: EXC_BAD_ACCESS (code=1, address=0x10)
Run Code Online (Sandbox Code Playgroud)

这是代码:

func endGame(r : String) {

    UIView.animateWithDuration(0.4, delay: 0.2, options: .CurveLinear, animations: {
        self.scoreLabel.alpha = 0
        }, completion: {
            (finished: Bool) in
            self.scoreLabel.removeFromSuperview()
    });

    self.view.userInteractionEnabled = false
    reason = r
    println("Game Over!!!")

    //Crashes on this line
    blockUpdateDisplayLink.removeFromRunLoop(NSRunLoop.currentRunLoop(), forMode: NSRunLoopCommonModes)

    shiftDisplayLink.removeFromRunLoop(NSRunLoop.currentRunLoop(), forMode: NSRunLoopCommonModes)

    scoreTimer.invalidate()

    UIView.animateWithDuration(0.0001, delay: 0.7, options: .CurveLinear, animations: {

        }, completion: {
            (finished: Bool) in
            self.performSegueWithIdentifier("Game Over", sender: self)
    });
}
Run Code Online (Sandbox Code Playgroud)

如果我注释掉第一个CADisplayLink部分,那么无论如何它都会在第二个部分崩溃.

这是堆栈跟踪:

在此输入图像描述

它与上面的"线程1"错误相同.

到底是怎么回事??

Dav*_*yle 5

您必须使显示链接无效(而不是仅仅从运行循环中删除它们),因为如果它们在激活时处于运行中,它们仍可能尝试使用其关联的运行循环.