Crash in [CAMetalLayer nextDrawable]

We are seeing a crash in [CAMetalLayer nextDrawable]. When reproducing in a debug build, I get the following message:


/BuildRoot/Library/Caches/com.apple.xbs/Sources/Metal/Metal-54.31/ToolsLayers/Debug/MTLDebugDevice.mm:629: failed assertion `iosurface must not be nil.'


We call nextDrawable from a secondary thread, where our rendering takes place. Any ideas on what could cause this? In the mean time, we are also changing the view hierachy by presenting a popover, so that might be related. The full crash log is attached:


Incident Identifier: DD463424-2C63-4CD6-A7D7-BDF4A9002A64
Beta Identifier:     95C4A6E9-1F66-4CA8-ACA3-F101440751CC
Hardware Model:      iPad4,5
Process:             Pocket Quest [425]
Path:                /private/var/mobile/Containers/Bundle/Application/E0DB9FEC-7679-429F-8148-D0569040E22C/Pocket Quest.app/Pocket Quest
Identifier:          com.firigames.pocketquest
Version:             7868 (0.6)
Beta:                YES
Code Type:           ARM-64 (Native)
Parent Process:      launchd [1]


Date/Time:           2015-11-10 10:47:16.16 +0100
Launch Time:         2015-11-10 10:46:49.49 +0100
OS Version:          iOS 9.0 (13A344)
Report Version:      105


Exception Type:  EXC_CRASH (SIGABRT)
Exception Codes: 0x0000000000000000, 0x0000000000000000
Exception Note:  EXC_CORPSE_NOTIFY
Triggered by Thread:  14


Global Trace Buffer (reverse chronological seconds):
25.270620    CFNetwork                 0x00000001846c59a4 TCP Conn 0x15e5b9dc0 SSL Handshake DONE
25.399860    CFNetwork                 0x00000001846c58b4 TCP Conn 0x15e5b9dc0 starting SSL negotiation
25.400145    CFNetwork                 0x0000000184767760 TCP Conn 0x15e5b9dc0 complete. fd: 15, err: 0
25.401487    CFNetwork                 0x0000000184768c8c TCP Conn 0x15e5b9dc0 event 1. err: 0
25.457940    CFNetwork                 0x0000000184768d64 TCP Conn 0x15e5b9dc0 started
26.338075    CFNetwork                 0x00000001846c59a4 TCP Conn 0x15e001bc0 SSL Handshake DONE
26.492871    CFNetwork                 0x00000001846c58b4 TCP Conn 0x15e001bc0 starting SSL negotiation
26.493456    CFNetwork                 0x0000000184767760 TCP Conn 0x15e001bc0 complete. fd: 9, err: 0
26.494894    CFNetwork                 0x0000000184768c8c TCP Conn 0x15e001bc0 event 1. err: 0
26.592204    CFNetwork                 0x0000000184768d64 TCP Conn 0x15e001bc0 started


Thread 0 name:
Thread 0:
0   CoreFoundation                 0x0000000184ea2200 CFEqual + 92 (CFRuntime.c:621)
1   CoreFoundation                 0x0000000185003964 __NSCacheKeyEqual + 12 (NSCache.m:82)
2   CoreFoundation                 0x0000000185003964 __NSCacheKeyEqual + 12 (NSCache.m:82)
3   libcache.dylib                 0x000000019a1debcc _entry_get_optionally_checking_collisions + 164 (cache.c:892)
4   libcache.dylib                 0x000000019a1dd1b8 cache_get_and_retain + 132 (cache.c:905)
5   CoreFoundation                 0x0000000184ef294c -[NSCache objectForKey:] + 68 (NSCache.m:198)
6   CoreUI                         0x000000018de1cfe0 -[CUIStructuredThemeStore _canGetRenditionWithKey:isFPO:lookForSubstitutions:] + 360 (CUIStructuredThemeStore.m:1203)
7   CoreUI                         0x000000018de3f1a0 +[CUICatalog _resolvedRenditionKeyFromThemeRef:withBaseKey:scaleFactor:deviceIdiom:deviceSubtype:sizeClassHorizontal:sizeClassVertical:memoryClass:graphicsClass:graphicsFallBackOrder:] + 1204 (CUICatalog.m:1626)
8   CoreUI                         0x000000018de3e438 -[CUICatalog namedLookupWithName:scaleFactor:deviceIdiom:deviceSubtype:sizeClassHorizontal:sizeClassVertical:] + 148 (CUICatalog.m:1286)
9   UIKit                         0x000000018ad2eac4 __98-[_UIAssetManager imageNamed:scale:idiom:subtype:cachingOptions:sizeClassPair:attachCatalogImage:]_block_invoke + 424 (_UIAssetManager.m:371)
10  UIKit                         0x000000018ad2e864 -[_UIAssetManager imageNamed:scale:idiom:subtype:cachingOptions:sizeClassPair:attachCatalogImage:] + 212 (_UIAssetManager.m:414)
11  UIKit                         0x000000018ad2f048 -[_UIAssetManager imageNamed:withTrait:] + 528 (_UIAssetManager.m:471)
12  UIKit                         0x000000018a7f4910 +[UIImage imageNamed:inBundle:compatibleWithTraitCollection:] + 220 (UIImage.m:926)
13  UIKit                         0x000000018a652cf8 +[UIImage imageNamed:] + 124 (UIImage.m:913)
14  Pocket Quest                   0x0000000100224bc4 0x1000f8000 + 1231812
15  Pocket Quest                   0x00000001001586d0 0x1000f8000 + 394960
16  Pocket Quest                   0x0000000100158584 0x1000f8000 + 394628
17  UIKit                         0x000000018a7777b4 -[_UIPopoverView initWithFrame:backgroundViewClass:embeddedInView:] + 352 (_UIPopoverView.m:67)
18  UIKit                         0x000000018adf42a4 __66-[UIPopoverPresentationController presentationTransitionWillBegin]_block_invoke494 + 104 (UIPopoverPresentationController.m:1266)
19  UIKit                         0x000000018a525dc0 +[UIView(Animation) performWithoutAnimation:] + 80 (UIView.m:8910)
20  UIKit                         0x000000018adf3d78 -[UIPopoverPresentationController presentationTransitionWillBegin] + 2132 (UIPopoverPresentationController.m:1265)
21  UIKit                         0x000000018a864254 __71-[UIPresentationController _initViewHierarchyForPresentationSuperview:]_block_invoke + 1616 (UIPresentationController.m:1128)
22  UIKit                         0x000000018a862384 __56-[UIPresentationController runTransitionForCurrentState]_block_invoke + 332 (UIPresentationController.m:696)
23  UIKit                         0x000000018a7c0844 _runAfterCACommitDeferredBlocks + 292 (UIApplication.m:2297)
24  UIKit                         0x000000018a7ccde4 _cleanUpAfterCAFlushAndRunDeferredBlocks + 92 (UIApplication.m:2275)
25  UIKit                         0x000000018a5091e4 _afterCACommitHandler + 96 (UIApplication.m:2327)
26  CoreFoundation                 0x0000000184f77c30 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 32 (CFRunLoop.c:1620)
27  CoreFoundation                 0x0000000184f759d4 __CFRunLoopDoObservers + 372 (CFRunLoop.c:1716)
28  CoreFoundation                 0x0000000184f75e04 __CFRunLoopRun + 928 (CFRunLoop.c:2558)
29  CoreFoundation                 0x0000000184ea4dc0 CFRunLoopRunSpecific + 384 (CFRunLoop.c:2814)
30  GraphicsServices               0x000000018fe34088 GSEventRunModal + 180 (GSEvent.c:2245)
31  UIKit                         0x000000018a57ef60 UIApplicationMain + 204 (UIApplication.m:3660)
32  Pocket Quest                   0x000000010012f868 0x1000f8000 + 227432
33  libdyld.dylib                 0x000000019a28a8b8 start + 4 (start_glue.s:80)


Thread 1 name:
Thread 1:
0   libsystem_kernel.dylib         0x000000019a3a6270 __bsdthread_ctl + 8
1   libsystem_pthread.dylib       0x000000019a472b38 _pthread_set_properties_self + 64 (qos.c:318)
2   libdispatch.dylib             0x000000019a267b08 _dispatch_root_queue_drain + 1972 (inline_internal.h:714)
3   libdispatch.dylib             0x000000019a26734c _dispatch_worker_thread3 + 112 (queue.c:4249)
4   libsystem_pthread.dylib       0x000000019a46d478 _pthread_wqthread + 1092 (pthread.c:1990)
5   libsystem_pthread.dylib       0x000000019a46d028 start_wqthread + 4 (pthread_asm.s:191)


Thread 2 name:
Thread 2:
0   libsystem_kernel.dylib         0x000000019a3a859c kevent_qos + 8
1   libdispatch.dylib             0x000000019a26ca04 _dispatch_mgr_invoke + 232 (source.c:2542)
2   libdispatch.dylib             0x000000019a25b93c _dispatch_source_invoke + 0 (source.c:777)


Thread 3:
0   libsystem_pthread.dylib       0x000000019a46d024 start_wqthread + 0 (pthread_asm.s:190)


Thread 4:
0   libsystem_kernel.dylib         0x000000019a3a7c0c __workq_kernreturn + 8
1   libsystem_pthread.dylib       0x000000019a46d538 _pthread_wqthread + 1284 (pthread.c:1999)
2   libsystem_pthread.dylib       0x000000019a46d028 start_wqthread + 4 (pthread_asm.s:191)


Thread 5 name:
Thread 5:
0   libsystem_kernel.dylib         0x000000019a38cc30 mach_msg_trap + 8 (syscall_sw.h:105)
1   libsystem_kernel.dylib         0x000000019a38caac mach_msg + 72 (mach_msg.c:103)
2   CoreFoundation                 0x0000000184f78168 __CFRunLoopServiceMachPort + 196 (CFRunLoop.c:2345)
3   CoreFoundation                 0x0000000184f75e6c __CFRunLoopRun + 1032 (CFRunLoop.c:2607)
4   CoreFoundation                 0x0000000184ea4dc0 CFRunLoopRunSpecific + 384 (CFRunLoop.c:2814)
5   libAVFAudio.dylib             0x0000000183611860 GenericRunLoopThread::Entry(void*) + 164 (GenericRunLoopThread.h:102)
6   libAVFAudio.dylib             0x00000001835e63a8 CAPThread::Entry(CAPThread*) + 84 (CAPThread.cpp:275)
7   libsystem_pthread.dylib       0x000000019a46fb3c _pthread_body + 156 (pthread.c:656)
8   libsystem_pthread.dylib       0x000000019a46faa0 _pthread_start + 156 (pthread.c:692)
9   libsystem_pthread.dylib       0x000000019a46d030 thread_start + 4 (pthread_asm.s:198)


Thread 6 name:
Thread 6:
0   libsystem_kernel.dylib         0x000000019a38cc30 mach_msg_trap + 8 (syscall_sw.h:105)
1   libsystem_kernel.dylib         0x000000019a38caac mach_msg + 72 (mach_msg.c:103)
2   AudioToolbox                   0x000000018426b0c8 AURemoteIO::IOThread::Run() + 112 (AURemoteIO.cpp:1495)
3   AudioToolbox                   0x000000018426ebf8 AURemoteIO::IOThread::Entry(void*) + 12 (AURemoteIO.h:279)
4   AudioToolbox                   0x0000000184178298 CAPThread::Entry(CAPThread*) + 124 (CAPThread.cpp:275)
5   libsystem_pthread.dylib       0x000000019a46fb3c _pthread_body + 156 (pthread.c:656)
6   libsystem_pthread.dylib       0x000000019a46faa0 _pthread_start + 156 (pthread.c:692)
7   libsystem_pthread.dylib       0x000000019a46d030 thread_start + 4 (pthread_asm.s:198)


Thread 7 name:
Thread 7:
0   libsystem_kernel.dylib         0x000000019a3a74e0 __semwait_signal + 8
1   libsystem_c.dylib             0x000000019a2c5e2c nanosleep + 212 (nanosleep.c:104)
2   libc++.1.dylib                 0x00000001990ee314 std::__1::this_thread::sleep_for(std::__1::chrono::duration<long long, std::__1::ratio<1l, 1000000000l> > const&) + 84 (thread.cpp:109)
3   Pocket Quest                   0x00000001001956e8 0x1000f8000 + 644840
4   libsystem_pthread.dylib       0x000000019a46fb3c _pthread_body + 156 (pthread.c:656)
5   libsystem_pthread.dylib       0x000000019a46faa0 _pthread_start + 156 (pthread.c:692)
6   libsystem_pthread.dylib       0x000000019a46d030 thread_start + 4 (pthread_asm.s:198)


Thread 8 name:
Thread 8:
0   libsystem_kernel.dylib         0x000000019a38cc30 mach_msg_trap + 8 (syscall_sw.h:105)
1   libsystem_kernel.dylib         0x000000019a38caac mach_msg + 72 (mach_msg.c:103)
2   CoreFoundation                 0x0000000184f78168 __CFRunLoopServiceMachPort + 196 (CFRunLoop.c:2345)
3   CoreFoundation                 0x0000000184f75e6c __CFRunLoopRun + 1032 (CFRunLoop.c:2607)
4   CoreFoundation                 0x0000000184ea4dc0 CFRunLoopRunSpecific + 384 (CFRunLoop.c:2814)
5   CFNetwork                     0x0000000184742074 +[NSURLConnection(Loader) _resourceLoadLoop:] + 412 (NSURLConnection.mm:325)
6   Foundation                     0x0000000185eff138 __NSThread__start__ + 1000 (NSThread.m:1134)
7   libsystem_pthread.dylib       0x000000019a46fb3c _pthread_body + 156 (pthread.c:656)
8   libsystem_pthread.dylib       0x000000019a46faa0 _pthread_start + 156 (pthread.c:692)
9   libsystem_pthread.dylib       0x000000019a46d030 thread_start + 4 (pthread_asm.s:198)


Thread 9 name:
Thread 9:
0   libsystem_kernel.dylib         0x000000019a3a7408 __select + 8
1   CoreFoundation                 0x0000000184f7e6d0 __CFSocketManager + 648 (CFSocket.c:2128)
2   libsystem_pthread.dylib       0x000000019a46fb3c _pthread_body + 156 (pthread.c:656)
3   libsystem_pthread.dylib       0x000000019a46faa0 _pthread_start + 156 (pthread.c:692)
4   libsystem_pthread.dylib       0x000000019a46d030 thread_start + 4 (pthread_asm.s:198)


Thread 10 name:
Thread 10:
0   libsystem_kernel.dylib         0x000000019a38cc30 mach_msg_trap + 8 (syscall_sw.h:105)
1   libsystem_kernel.dylib         0x000000019a38caac mach_msg + 72 (mach_msg.c:103)
2   CoreFoundation                 0x0000000184f78168 __CFRunLoopServiceMachPort + 196 (CFRunLoop.c:2345)
3   CoreFoundation                 0x0000000184f75e6c __CFRunLoopRun + 1032 (CFRunLoop.c:2607)
4   CoreFoundation                 0x0000000184ea4dc0 CFRunLoopRunSpecific + 384 (CFRunLoop.c:2814)
5   AudioToolbox                   0x0000000184185ee8 GenericRunLoopThread::Entry(void*) + 164 (GenericRunLoopThread.h:102)
6   AudioToolbox                   0x0000000184178298 CAPThread::Entry(CAPThread*) + 124 (CAPThread.cpp:275)
7   libsystem_pthread.dylib       0x000000019a46fb3c _pthread_body + 156 (pthread.c:656)
8   libsystem_pthread.dylib       0x000000019a46faa0 _pthread_start + 156 (pthread.c:692)
9   libsystem_pthread.dylib       0x000000019a46d030 thread_start + 4 (pthread_asm.s:198)


Thread 11 name:
Thread 11:
0   Pocket Quest                   0x00000001002bf244 0x1000f8000 + 1864260
1   Pocket Quest                   0x00000001001dffa4 0x1000f8000 + 950180
2   Pocket Quest                   0x0000000100161ce0 0x1000f8000 + 433376
3   Pocket Quest                   0x0000000100167048 0x1000f8000 + 454728
4   libsystem_pthread.dylib       0x000000019a46fb3c _pthread_body + 156 (pthread.c:656)
5   libsystem_pthread.dylib       0x000000019a46faa0 _pthread_start + 156 (pthread.c:692)
6   libsystem_pthread.dylib       0x000000019a46d030 thread_start + 4 (pthread_asm.s:198)


Thread 12:
0   libsystem_kernel.dylib         0x000000019a3a7c0c __workq_kernreturn + 8
1   libsystem_pthread.dylib       0x000000019a46d538 _pthread_wqthread + 1284 (pthread.c:1999)
2   libsystem_pthread.dylib       0x000000019a46d028 start_wqthread + 4 (pthread_asm.s:191)


Thread 13:
0   libsystem_kernel.dylib         0x000000019a3a7c0c __workq_kernreturn + 8
1   libsystem_pthread.dylib       0x000000019a46d538 _pthread_wqthread + 1284 (pthread.c:1999)
2   libsystem_pthread.dylib       0x000000019a46d028 start_wqthread + 4 (pthread_asm.s:191)


Thread 14 name:
Thread 14 Crashed:
0   libsystem_kernel.dylib         0x000000019a3a71e0 __pthread_kill + 8
1   libsystem_pthread.dylib       0x000000019a470f0c pthread_kill + 112 (pthread.c:1247)
2   libsystem_c.dylib             0x000000019a31ab78 abort + 140 (abort.c:91)
3   libsystem_c.dylib             0x000000019a2efb28 __assert_rtn + 284 (assert.c:91)
4   Metal                         0x0000000187cd929c MTLReportFailure + 480 (MTLDebug.m:220)
5   Metal                         0x0000000187cbdff4 -[MTLTextureDescriptorInternal validateWithDevice:] + 584 (MTLTexture.m:619)
6   AGXMetalG3                     0x0000000104f0d4b4 -[AGXTexture initWithDevice:desc:iosurface:plane:] + 80 (agxa_texture_obj.mm:136)
7   QuartzCore                     0x0000000189e56368 allocate_drawable_private(unsigned int, unsigned int, id<MTLDevice>, MTLPixelFormat, MTLTextureRotation, bool, bool, bool, unsigned int, _CAImageQueue*) + 948 (CAMetalLayer.mm:330)
8   QuartzCore                     0x0000000189e55b44 -[CAMetalLayer nextDrawable] + 412 (CAMetalLayer.mm:393)
9   Pocket Quest                   0x00000001001eee00 0x1000f8000 + 1011200
10  Pocket Quest                   0x0000000100166c60 0x1000f8000 + 453728
11  Pocket Quest                   0x0000000100161f98 0x1000f8000 + 434072
12  Pocket Quest                   0x0000000100167048 0x1000f8000 + 454728
13  libsystem_pthread.dylib       0x000000019a46fb3c _pthread_body + 156 (pthread.c:656)
14  libsystem_pthread.dylib       0x000000019a46faa0 _pthread_start + 156 (pthread.c:692)
15  libsystem_pthread.dylib       0x000000019a46d030 thread_start + 4 (pthread_asm.s:198)


Thread 14 crashed with ARM Thread State (64-bit):
    x0: 0x0000000000000000   x1: 0x0000000000000000   x2: 0x0000000000000000   x3: 0x0000000000000000
    x4: 0x0000000000000000   x5: 0x000000000000001f   x6: 0x0000000000000000   x7: 0x0000000000000000
    x8: 0x0000000008000000   x9: 0x0000000004000000  x10: 0x0000000000000000  x11: 0x0000000000000036
   x12: 0x000000015d813b10  x13: 0x000001a1a0bf2ac1  x14: 0xffffffffffffc000  x15: 0x0000000000003fff
   x16: 0x0000000000000148  x17: 0x0000000184eb09a4  x18: 0x0000000000000000  x19: 0x0000000000000006
   x20: 0x000000016e503000  x21: 0x000000000000026b  x22: 0x00000001072cc000  x23: 0x00000001a0bcf000
   x24: 0x000000019fe97000  x25: 0x0000000000000000  x26: 0x000000016e502ac0  x27: 0x000000015e5e5d70
   x28: 0x0000000000000000  fp: 0x000000016e5027a0   lr: 0x000000019a470f0c
    sp: 0x000000016e502780   pc: 0x000000019a3a71e0 cpsr: 0x00000000


Binary Images:
<omitted>

I'm having the same problem! I'm using swift however but whenever I call

metalLayer.nextDrawable()

I get the same error. Here's my code, pretty simple example just supposed to change the background color.

import UIKit
class TSTMetalView: UIView {
   
    override class func layerClass() -> AnyClass {
        return CAMetalLayer.self
    }
   
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
   
    override func didMoveToWindow() {
        self.redraw()
    }
   
    func redraw() {
        var device = MTLCreateSystemDefaultDevice()
        var metalLayer = CAMetalLayer()
        metalLayer.device = device
        metalLayer.pixelFormat = .BGRA8Unorm
       
        var drawable = metalLayer.nextDrawable()
        var texture = drawable?.texture
       
        var commandQueue = device?.newCommandQueue()
       
        let passDescriptor: MTLRenderPassDescriptor = MTLRenderPassDescriptor.self()
        passDescriptor.colorAttachments[0].texture = texture
        passDescriptor.colorAttachments[0].loadAction = MTLLoadAction.Clear
        passDescriptor.colorAttachments[0].storeAction = MTLStoreAction.Store
        passDescriptor.colorAttachments[0].clearColor = MTLClearColor.init(red: 1, green: 0, blue: 0, alpha: 1)
       
        let commandBuffer: MTLCommandBuffer = commandQueue!.commandBuffer()
       
        let commandEncoder: MTLCommandEncoder = commandBuffer.renderCommandEncoderWithDescriptor(passDescriptor)
        commandEncoder.endEncoding()
       
        commandBuffer.presentDrawable(drawable!)
        commandBuffer.commit()
       
    }
}

The old forums had some decent interaction with Apple engineers. These new ones are just dead. What is going on?

Did you ever resolve this? I am having the same issue in Swift.

This error often occurs when the size of the layer is (0, 0). By default, a layer's frame is set to

CGRectZero
, and in the case of
CAMetalLayer
, its backing
IOSurface
cannot be allocated until its size is set to a non-zero value. You should also ensure that the layer is actually added to the layer hierarchy of the view, either explicitly with the
-addSublayer:
method or by using layer backing.

Thanks! This helped me resolve the issue. The "iosurface must not be nil" error was occurring because my MTKView's frame and layer size was 0 height and 0 width, just like you said.


I had an MTKView in my Storyboard with some contraints setup. My constraints were messed up, which resulted in my MTKView frame to have 0 height and 0 width. After correcting the constraints, my MTKView had a non-zero height/width frame size, and the "iosurface must not be nil" error stopped occurring.

Crash in [CAMetalLayer nextDrawable]
 
 
Q