[CaptureMTLDepthStencilState retain] crash during Debug run

I am having trouble finding information about CaptureMTLDepthStencilState but we are seeing the following error message (logged by enabling "Zombie Objects") after a sporadic crash when running our app (Debug build with "Debug executable" enabled) on device without breakpoints.

Code Block
[CaptureMTLDepthStencilState retain]: message sent to deallocated instance


We are not attempting frame capture when this happens and this crash always occurs during a call to newDepthStencilStateWithDescriptor.

macOS 11.2.2
Xcode 12.4 (12D4e)
iOS 14.4

Does anyone have any experience with this or insight into what might be causing it?
Hi Collen.

Do you ever call -[MTLCommandQueue [commandBufferWithUnretainedReferences]. If so this could be due to your app releasing the depth stencil state object before a command buffer using it is complete.

Otherwise it's likely a bug in the GPU capture tools. If you created a request via Feedback Assistant and attach a reproducer that would be appreciated. You can probably work around this by disabling Metal captures in the run scheme under "Options".


I also did some investigation, and after adding some code to ensure that any MTLDepthStencilState objects are held until the next frame, i got a different crash:


Code Block
2021-04-22 11:18:13.300307-0700 MSDAPIExampleApp[1357:293877] -[MTLDepthStencilDescriptorInternal touch]: unrecognized selector sent to instance 0x28089a010
2021-04-22 11:18:13.303443-0700 MSDAPIExampleApp[1357:293558] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[MTLDepthStencilDescriptorInternal touch]: unrecognized selector sent to instance 0x28089a010'
* First throw call stack:
(0x19847186c 0x1ad48ac50 0x19837895c 0x198474438 0x198476740 0x109406524 0x102cd3a54 0x103117cec 0x103117934 0x1031c5f3c 0x10301b8ac 0x102f7dd04 0x102f7dc4c 0x102f7dbbc 0x102f7db70 0x102f7d6f4 0x102f7f9ac 0x102f7f880 0x102f7f81c 0x102f7f644 0x103376244 0x10337b90c 0x10337b890 0x10337b83c 0x10337b7f4 0x10337b7a8 0x10337b780 0x10337a444 0x1037b81fc 0x1037b5ef8 0x103040614 0x1030405cc 0x103040580 0x103040558 0x10303efe8 0x1037b81fc 0x1037b5ef8 0x1031087e0 0x10303e5d8 0x102d915f0 0x102d9196c 0x102d9876c 0x102d92060 0x102f6c61c 0x102f6c20c 0x102f6c0a8 0x102f6bfcc 0x102f6bf2c 0x102f6b620 0x1e3f1bcb0 0x1e3f24778)
libc++abi.dylib: terminating with uncaught exception of type NSException
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[MTLDepthStencilDescriptorInternal touch]: unrecognized selector sent to instance 0x28089a010'
terminating with uncaught exception of type NSException


touch appears to be a selector that rides on CaptureMTLDepthStencilState, but its getting called on the internal descriptor in this case.

My best guess at the cause if you want to dig in is a suspicious call to objc_getAssociatedObject. It seems like it might be a memory corruption issue where the id returned is either freed in some cases, causing the exc_bad_access, or corrupted in some cases causing the unrecognized selector error.

Unfortunately debugging this without source or symbols was pretty difficult, so I wasn't able to get much further, but I was able to dodge the issue by disabling metal frame capture. Ideally we want to have this capability in our app, as it's invaluable for debugging actual metal issues. Is there some ETA for a fix, or a way to get notified of any progress on your end? I'd be more than willing to help test any fixes you could provide.
[CaptureMTLDepthStencilState retain] crash during Debug run
 
 
Q