4

This doesn't happen every time you play the game, maybe once for every 5 or 10 plays. When the game ends, I remove my CADisplayLink (which I use to animate the playing area, a bit like the pipes in Flappy Bird) from the run loop. However, on the few occasions, it crashes on that line. Next to 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 out the first CADisplayLink part, it will just crash on the second anyway.

This is the stacktrace:

enter image description here

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

What is going on??

user2397282
  • 3,798
  • 15
  • 48
  • 94

3 Answers3

5

You must invalidate display links (rather than just removing them from the run loop) because if they are in the middle of running when they activate they may still try to use their associated run loop.

David Doyle
  • 1,716
  • 10
  • 23
4

Which run loop are you adding the CADisplayLink to? You might need to be using NSRunLoop.mainRunLoop() instead.

Also, if your CADisplayLink is only being added on one NSRunLoop, you could try calling blockUpdateDisplayLink.invalidate() instead of removing it.


If you post the code where you create the CADisplayLink objects, it will make it easier to track down the issue.

Craig Siemens
  • 12,942
  • 1
  • 34
  • 51
1

I will really suggest you to debug you're code with Instruments and NSZombies and find out your memory issue problem.

Instruments

NSZombies

Community
  • 1
  • 1
Oleg Gordiichuk
  • 15,240
  • 7
  • 60
  • 100