Crash in 4.17.2 with instanced static meshes (Unknown RHI vertex element type 0)

Hello, I am using some simple C++ code for creating runtime instanced static mesh components.
My code worked great until 4.17.1, but since I upgraded to 4.17.2 today it just crashes.
Since I was unable to understand much (callstack at the end) I also tried upgrading to 4.18 preview but the crash still occurred.
My project is quite extensive so I don’t think I can give exact steps to reproduce but I will include the code that if I comment out stops the crash.
This is how I create my component: (this is done at runtime)

NewComp = NewObject<UInstancedStaticMeshComponent>(this, name);
NewComp->SetStaticMesh(mesh);
NewComp->AttachToComponent(PlanetCenter, FAttachmentTransformRules::SnapToTargetIncludingScale);
NewComp->RegisterComponent();

and this is how I add an instance again during runtime - commenting this out stops the crash (even if the component is created)

Comp->AddInstance(FTransform(rot, loc, scale));

I guess that this is common stuff.
Now the crash happens in Windows\D3D11RHI\Private\D3D11VertexDeclaration.cpp, line 54 (“Unknown RHI vertex element type 0”)
Full stack trace here: > [Inline Frame] UE4Editor-D3D11RHI.dll!FWindowsPlatformMisc::DebugBreak() Line - Pastebin.com

I tried switching my mesh just in case my materials were a problem - but it still happens everytime.

I would appreciate any help, even a workaround, since my game is broken atm…

Thanks in advance

Hello! I’m having the same problem. Found a workaround, just call the following functions right after the call to RegisterComponent:

NewComp->ReleasePerInstanceRenderData();
NewComp->InitPerInstanceRenderData();

It seems the InstanceRenderData is not created before RHI initialization in some cases, leading to vertex declaration referencing NULL vertex buffers.

Wow! Nice find! Yeah, I could make it work with that! Thanks a lot!
Nevertheless, could we have some info about why this is happening? Is this going to be fixed?

The funny thing is: If one adds the instanced static mesh component in a Blueprint instead of C++ it just works…

Hey -

As mentioned in another post here (Crash when adding an instance with Instanced Static Mesh in 4.17.2 - Programming & Scripting - Epic Developer Community Forums) this issue does occur in 4.17.2 but has been fixed internally and will be included with an update to the engine.

Cheers

Yes, I saw the post! Thank you, I will be using the workaround mentioned above until said update

This is currently fixed in 4.18, so you can have the fix in the next preview.