Crash in UMaterialInstance::AddReferencedObjects

Hi,

After upgrading our project to 4.6 I’ve been getting a crash in UMaterialInstance::AddReferencedObjects. Certain ULandscapeMaterialInstanceConstant objects have bHasStaticPermutationResource set to true, but StaticPermutationMaterialResources has NULL objects in it (which according to the comments in the code is never supposed to happen).

As far as I can tell the reason this is possible is because bHasStaticPermutationResource can be serialized in as true during object creation (streaming in a piece of landscape), but before UMaterialInstance::PostLoad is called (which initializes StaticPermutationMaterialResources) the garbage collector running on another thread can call UMaterialInstance::AddReferencedObjects.

I’ve fixed this locally by just checking CurrentResource is not NULL before AddReferencedObjects is called on it, but I also wonder if bHasStaticPermutationResource should be serialized, probably not. It should be false until the appropriate code determines if it should be true or not. Even so there are no protections in the code to syncronize the GC thread with the setting of bHasStaticPermutationResource to true and initializing StaticPermutationMaterialResources (the render thread that uses StaticPermutationMaterialResources does have such protections).

Cheers,

Ryan.

Hi LuaChunk,

Sorry for the delay in responding to your post. Do you have any specific information for creating a repro for this issue? Is this something that you are able to recreate in a new project?

Unfortunately reproducing it is very difficult for two reasons:

  1. It’s basically a race condition between the GC thread and the system that streams/serializes in new objects. Theoretically it can never happen if the timings just don’t work out. I’d say I just got lucky/unlucky with how our level is setup.
  2. I’m not familiar enough with the terrain system to know what circumstances lead to bHasStaticPermutationResource being set to true so I don’t know how to make a test level to trigger this. Our game does have a terrain in it, but I don’t know what we’ve done to it to cause bHasStaticPermutationResource to be set to true. You’d have to talk to the programmer that implemented the terrain system I think.

Sorry I can’t be of more help, it’s a very obscure bug :-/

My suggested fix of checking CurrentResource is not NULL is in 4.7.1, so this is solved now.

Thanks!