Aleksandar • Vacić

iOS bits and pieces

Removing CADisplayLink from run loop

Do not use this

1
2
[self.displayLink removeFromRunLoop:NSRunLoop.mainRunLoop
                            forMode:NSRunLoopCommonModes];

Instead use this:

1
[self.displayLink invalidate];

Reason, from documentation:

Removing the display link from all run loop modes causes it to be released by the run loop. The display link also releases the target.

It safely releases all references and removes itself.

I was recently chasing a very strange rare crashes in Run 5k, my app with 1000s of daily users. It would SIGSEGV crash inside the CADisplayLink’s selector for about 100 or them during the week. Making the code change above solved all those issues.

My guess is that removeFromRunLoop:forMode: would enter a race condition with the call to the target’s selector method in some obscure situation. Who knows.