The game sometimes crashes when removing CADisplayLink from the launch loop

It doesn't happen every time you play the game, maybe once every 5 or 10 plays. When the game ends, I remove my CADisplayLink (which I use to animate the playground, a bit like the pipes in Flappy Bird) from the launch loop. However, in some cases, it falls on this line. Beside the line, it has:

Thread 1: EXC_BAD_ACCESS (code=1, address=0x10)

      

This is the code:

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

      

if I comment on the first part of CADisplayLink, it still crashes in the second.

This is the stacktrace:

enter image description here

Which has the same "Thread 1" error as above.

What's happening?

+3


source to share


3 answers


Which cycle are you adding CADisplayLink

to? You may need to use NSRunLoop.mainRunLoop()

.

Also, if yours CADisplayLink

is only added to one NSRunLoop

, you can try calling blockUpdateDisplayLink.invalidate()

instead of removing it.




Putting the code where you create objects CADisplayLink

will make it easier to track down the issue.

+3


source


You should invalidate the referenced links (and not just remove them from the run loop) because if they are in the middle of the run when they get fired, they might still try to use their associated run loop.



+4


source


I really suggest you debug your code with tools and NSZombies and inquire about the memory issue.

Instruments

NSZombies

+1


source







All Articles