MTLDepthStencilState is getting deleted.

Hello,


Im am currently creating a metal renderer for our games.

I keep getting a crash that happens a few random seconds after the game has been running.


The exception occurs in [MTLRenderCommandEncoder setVertexBuffer]


{
     ....
     [activePass.rce setVertexBuffer:hmm->getMetalVertexBuffer() offset:0 atIndex:0];
     ....
}


When debugging with zombie objects i get this output:


Thread 1: EXC_BREAKPOINT (code=1, subcode=0x21c47b6f4)


*** -[MTLDebugDepthStencilState isEqual:]: message sent to deallocated instance 0x2805bbe80


The depth stencil state of the render encoder seems to be getting dealocated.


The depth stencil state is being set directly with this code:


    {
        depthStencilDescriptor = [MTLDepthStencilDescriptor new];
        [depthStencilDescriptor setDepthWriteEnabled: bDepthWrite];
        [depthStencilDescriptor setDepthCompareFunction: bDepthTest ? metalDepthCompareFunc : MTLCompareFunctionAlways];
        [activePass.rce setDepthStencilState:[metalDevice newDepthStencilStateWithDescriptor:depthStencilDescriptor]];
    }


The code is contained in a c++ class compiled as objective-c++


activePass is defined as:


    struct sPassInfo
    {
    public:
        id rce;
        MTLRenderPassDescriptor *pd;
        zRenderPass rp;
    };


Has anyone had any memory related problems with metal while compiling it as c++?


Any information would be appreciated, thanks ^_^

Replies

Might be a bug in metal validation layer. Try disabling in scheme settings in XCode

I'm running into this as well.


Disabling Metal validation in the scheme (as suggested) doesn't work, but the crash only seems to happen when running via XCode. A debug build running without the debugger attached is fine.


Also, I only see the problem on iOS--an iPhone 8 and 2nd gen iPad Pro in my case. The same code running on Mac OS doesn't hit it.


My project is C/Objective-C and ARC is disabled for the code in question. For what it's worth, autoreleasing the MTLDepthStencilState instead of releasing it immediately after encoding it shows the same behaviour.

Could also be Metal GPU capture (you can find an option to turn it off right above one for validation). Both these options inject some code between your app and Metal. In my experience (macOS only), this code is quite buggy.

In the past couple days I've apparently done something to fix this. The main thing I've been working on is enabling multisampling support, which has primarily involved some changes to render target setup. I don't see anything obvious that would affect depth/stencil state, but who knows what the validation code is actually doing.

Yeah, seems to do some really shady stuff under the hood in order for frame capturing to work. My guess is that the crash is occurring for a depth/stencil state which you set but never actually draw with.