[CAMetalLayer nextDrawable] random crash with bad access

Randomly it will crash while calling nextDrawable with one of those undescriptive bad access messages.


How would I go about trying to find the cause? The crash rarely happens, but it's common enough that I see a small number of them almost every day or so.


I have an autorelease pool, and the properties don't change, so what could it be?

Accepted Reply

After turning on the zombie objects, it became clear the root cause of the error had nothing to do nextDrawable. It was a pointer offset that was injecting nonsense data into random variables. Just by chance, those specific addresses were most often at some position related to the drawable.

Replies

Is it crashing on the call to nextDrawable or when you try to dereference the pointer returned? The docs for that method do say it can return nil...


a long GPU frame time could temporarily exhaust the available drawables and force this call to block until GPU rendering is complete. A call to this method will time out after 1 second if no drawable becomes available during this interval


Maybe apps need to handle this by skipping that frame and trying again later. This is one thing I like about Swift - that type is `CAMetalDrawable?` so it forces you to deal with the nil case.

It crashes when nextDrawable is called before it runs the nil check.


malloc: *** error for object 0x1047009e8: incorrect checksum for freed object - object was probably modified after being freed.

After turning on the zombie objects, it became clear the root cause of the error had nothing to do nextDrawable. It was a pointer offset that was injecting nonsense data into random variables. Just by chance, those specific addresses were most often at some position related to the drawable.